<!DOCTYPE html>
<html lang="zh-CN">
  <head>
  <meta charset="UTF-8">
  <meta 
    name="viewport"
    content="width=device-width, initial-scale=1.0, minimum-scale=1.0">
  <meta 
    http-equiv="X-UA-Compatible" 
    content="ie=edge">
  <meta 
    name="theme-color" 
    content="#fff" 
    id="theme-color">
  <meta 
    name="description" 
    content="hexoDemo用于介绍如何使用hexo">
  <link 
    rel="icon" 
    href="/">
  <title>07-浮动</title>
  
    
      <meta 
        property="og:title" 
        content="07-浮动">
    
    
      <meta 
        property="og:url" 
        content="https://zwl5670.gitee.io/2002/01/01/02-CSS%E5%9F%BA%E7%A1%80/07-%E6%B5%AE%E5%8A%A8/index.html">
    
    
      <meta 
        property="og:img" 
        content="https://img.songhn.com/img/Y67gdd.png">
    
    
      <meta 
        property="og:img" 
        content="hexoDemo用于介绍如何使用hexo">
    
    
      <meta 
        property="og:type" 
        content="article">
      <meta 
        property="og:article:published_time" 
        content="2002-01-01">
      <meta 
        property="og:article:modified_time" 
        content="2023-03-14">
      <meta 
        property="og:article:author" 
        content="zwl5670">
      
        
          <meta 
            property="og:article:tag" 
            content="CSS基础">
        
      
    
  
  
  <link rel="preload" href="//at.alicdn.com/t/font_1946621_i1kgafibvw.css" as="style" >
  <link rel="preload" href="//at.alicdn.com/t/font_1952792_89b4ac4k4up.css" as="style" >
  <link rel="preload" href="/css/main.css" as="style" >
  
  <link rel="modulepreload" href="//instant.page/5.1.0">
  
  <link rel="stylesheet" href="/css/main.css">
  
  <link rel="stylesheet" href="//at.alicdn.com/t/font_1946621_i1kgafibvw.css">
  
  <link rel="stylesheet" href="//at.alicdn.com/t/font_1952792_89b4ac4k4up.css">
  
  
  
    <link rel="stylesheet" href="/js/lib/lightbox/baguetteBox.min.css">
  
  <script>
    function loadScript(url, cb) {
      var script = document.createElement('script');
      script.src = url;
      if (cb) script.onload = cb;
      script.async = true;
      document.body.appendChild(script);
    }
    function loadCSS(href, data, attr) {
      var sheet = document.createElement('link');
      sheet.ref = 'stylesheet';
      sheet.href = href;
      sheet.dataset[data] = attr;
      document.head.appendChild(sheet);
    }
    function changeCSS(cssFile, data, attr) {
      var oldlink = document.querySelector(data);
      var newlink = document.createElement("link");
      newlink.setAttribute("rel", "stylesheet");
      newlink.setAttribute("href", cssFile);
      newlink.dataset.prism = attr;
      document.head.replaceChild(newlink, oldlink);
    }
  </script>
  
    
  
  <script>
    // control reverse button
    var reverseDarkList = {
      dark: 'light',
      light: 'dark'
    };
    var themeColor = {
      dark: '#1c1c1e',
      light: '#fff'
    }
    // get the data of css prefers-color-scheme
    var getCssMediaQuery = function() {
      return window.matchMedia('(prefers-color-scheme: dark)').matches ? 'dark' : 'light';
    };
    // reverse current darkmode setting function
    var reverseDarkModeSetting = function() {
      var setting = localStorage.getItem('user-color-scheme');
      if(reverseDarkList[setting]) {
        setting = reverseDarkList[setting];
      } else if(setting === null) {
        setting = reverseDarkList[getCssMediaQuery()];
      } else {
        return;
      }
      localStorage.setItem('user-color-scheme', setting);
      return setting;
    };
    // apply current darkmode setting
  </script>
  
    <script>
      var setDarkmode = function(mode) {
      var setting = mode || localStorage.getItem('user-color-scheme');
      if(setting === getCssMediaQuery()) {
        document.documentElement.removeAttribute('data-user-color-scheme');
        localStorage.removeItem('user-color-scheme');
        document.getElementById('theme-color').content = themeColor[setting];
        document.getElementById('theme-color').dataset.mode = setting;
      } else if(reverseDarkList[setting]) {
        document.documentElement.setAttribute('data-user-color-scheme', setting);
        document.getElementById('theme-color').content = themeColor[setting];
        document.getElementById('theme-color').dataset.mode = setting;
      } else {
        document.documentElement.removeAttribute('data-user-color-scheme');
        localStorage.removeItem('user-color-scheme');
        document.getElementById('theme-color').content = themeColor[getCssMediaQuery()];
        document.getElementById('theme-color').dataset.mode = getCssMediaQuery();
      }
    };
    setDarkmode();
    </script>
  
  
  
    <link rel="preload" href="/js/lib/lightbox/baguetteBox.min.js" as="script">
    <link rel="preload" href="/js/lib/lightbox/baguetteBox.min.css" as="style" >
  
  
    <link rel="preload" href="/js/lib/lozad.min.js" as="script">
  
  
  
  
  
  
<meta name="generator" content="Hexo 6.3.0"></head>

  <body>
    <div class="wrapper">
       
      <nav class="navbar">
  <div class="navbar-logo">
    <a class="navbar-logo-main" href="/">
      
      <span class="navbar-logo-dsc">前端面试</span>
      </a>
  </div>
  <div class="navbar-menu">
    
      <a 
        href="/" 
        class="navbar-menu-item">
        
          首页
        
      </a>
    
      <a 
        href="/archives" 
        class="navbar-menu-item">
        
          归档
        
      </a>
    
      <a 
        href="/tags" 
        class="navbar-menu-item">
        
          标签
        
      </a>
    
      <a 
        href="/categories" 
        class="navbar-menu-item">
        
          分类
        
      </a>
    
      <a 
        href="/about" 
        class="navbar-menu-item">
        
          关于
        
      </a>
    
      <a 
        href="/links" 
        class="navbar-menu-item">
        
          友链
        
      </a>
    
    <button 
      class="navbar-menu-item darknavbar navbar-menu-btn" 
      aria-label="Toggle dark mode"
      id="dark">
      <i class="iconfont icon-weather"></i>
    </button>
    <button 
      class="navbar-menu-item searchnavbar navbar-menu-btn" 
      aria-label="Toggle search"
      id="search">
      <!-- <i 
        class="iconfont icon-search" 
        style="font-size: 1.2rem; font-weight: 400;">
      </i> -->
      <svg xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" role="img"
        class="iconify iconify--ion" width="28" height="28" preserveAspectRatio="xMidYMid meet" viewBox="0 0 512 512">
        <path fill="none" stroke="currentColor" stroke-miterlimit="10" stroke-width="28"
          d="M256 80a176 176 0 1 0 176 176A176 176 0 0 0 256 80Z"></path>
        <path fill="none" stroke="currentColor" stroke-miterlimit="10" stroke-width="28"
          d="M232 160a72 72 0 1 0 72 72a72 72 0 0 0-72-72Z"></path>
        <path fill="none" stroke="currentColor" stroke-linecap="round" stroke-miterlimit="10" stroke-width="28"
          d="M283.64 283.64L336 336"></path>
      </svg>
    </button>
  </div>
</nav> 
      
      <div 
        id="local-search" 
        style="display: none">
        <input
          class="navbar-menu-item"
          id="search-input"
          placeholder="请输入搜索内容..." />
        <div id="search-content"></div>
      </div>
      
      <div class="section-wrap">
        <div class="container">
          <div class="columns">
            <aside class="left-column">
              
              <div class="card card-author">
                
  <img 
    src="https://img.songhn.com/img/Y67gdd.png" 
    class="author-img"
    width="88"
    height="88"
    alt="author avatar">

<p class="author-name">zwl5670</p>
<p class="author-description">前端学习资料</p>
<div class="author-message">
  <a 
    class="author-posts-count" 
    href="/archives">
    <span>215</span>
    <span>文章</span>
  </a>
  <a 
    class="author-categories-count" 
    href="/categories">
    <span>14</span>
    <span>分类</span>
  </a>
  <a 
    class="author-tags-count" 
    href="/tags">
    <span>14</span>
    <span>标签</span>
  </a>
</div>

              </div>
               <div class="sticky-tablet">
  
  
    <article class="display-when-two-columns spacer">
      <div class="card card-content toc-card">
        <div class="toc-header">
  <i 
    class="iconfont icon-menu" 
    style="padding-right: 2px;">
  </i>目录
</div>
<ol class="toc"><li class="toc-item toc-level-2"><a class="toc-link" href="#%E6%96%87%E6%9C%AC%E4%B8%BB%E8%A6%81%E5%86%85%E5%AE%B9"><span class="toc-text">文本主要内容</span></a></li><li class="toc-item toc-level-2"><a class="toc-link" href="#%E6%A0%87%E5%87%86%E6%96%87%E6%A1%A3%E6%B5%81"><span class="toc-text">标准文档流</span></a><ol class="toc-child"><li class="toc-item toc-level-3"><a class="toc-link" href="#%E6%A0%87%E5%87%86%E6%96%87%E6%A1%A3%E6%B5%81%E7%9A%84%E7%89%B9%E6%80%A7"><span class="toc-text">标准文档流的特性</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#%E8%A1%8C%E5%86%85%E5%85%83%E7%B4%A0%E5%92%8C%E5%9D%97%E7%BA%A7%E5%85%83%E7%B4%A0"><span class="toc-text">行内元素和块级元素</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#%E8%A1%8C%E5%86%85%E5%85%83%E7%B4%A0%E5%92%8C%E5%9D%97%E7%BA%A7%E5%85%83%E7%B4%A0%E7%9A%84%E7%9B%B8%E4%BA%92%E8%BD%AC%E6%8D%A2"><span class="toc-text">行内元素和块级元素的相互转换</span></a><ol class="toc-child"><li class="toc-item toc-level-4"><a class="toc-link" href="#%E5%9D%97%E7%BA%A7%E5%85%83%E7%B4%A0%E5%8F%AF%E4%BB%A5%E8%BD%AC%E6%8D%A2%E4%B8%BA%E8%A1%8C%E5%86%85%E5%85%83%E7%B4%A0%EF%BC%9A"><span class="toc-text">块级元素可以转换为行内元素：</span></a></li><li class="toc-item toc-level-4"><a class="toc-link" href="#%E8%A1%8C%E5%86%85%E5%85%83%E7%B4%A0%E8%BD%AC%E6%8D%A2%E4%B8%BA%E5%9D%97%E7%BA%A7%E5%85%83%E7%B4%A0%EF%BC%9A"><span class="toc-text">行内元素转换为块级元素：</span></a></li></ol></li></ol></li><li class="toc-item toc-level-2"><a class="toc-link" href="#%E6%B5%AE%E5%8A%A8%E7%9A%84%E6%80%A7%E8%B4%A8"><span class="toc-text">浮动的性质</span></a><ol class="toc-child"><li class="toc-item toc-level-3"><a class="toc-link" href="#%E6%80%A7%E8%B4%A81%EF%BC%9A%E6%B5%AE%E5%8A%A8%E7%9A%84%E5%85%83%E7%B4%A0%E8%84%B1%E6%A0%87"><span class="toc-text">性质1：浮动的元素脱标</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#%E6%80%A7%E8%B4%A82%EF%BC%9A%E6%B5%AE%E5%8A%A8%E7%9A%84%E5%85%83%E7%B4%A0%E4%BA%92%E7%9B%B8%E8%B4%B4%E9%9D%A0"><span class="toc-text">性质2：浮动的元素互相贴靠</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#%E6%80%A7%E8%B4%A83%EF%BC%9A%E6%B5%AE%E5%8A%A8%E7%9A%84%E5%85%83%E7%B4%A0%E6%9C%89%E2%80%9C%E5%AD%97%E5%9B%B4%E2%80%9D%E6%95%88%E6%9E%9C"><span class="toc-text">性质3：浮动的元素有“字围”效果</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#%E6%80%A7%E8%B4%A84%EF%BC%9A%E6%94%B6%E7%BC%A9"><span class="toc-text">性质4：收缩</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#%E6%B5%AE%E5%8A%A8%E7%9A%84%E8%A1%A5%E5%85%85%EF%BC%88%E5%81%9A%E7%BD%91%E7%AB%99%E6%97%B6%E6%B3%A8%E6%84%8F%EF%BC%89"><span class="toc-text">浮动的补充（做网站时注意）</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#%E5%B8%83%E7%BD%AE%E4%B8%80%E4%B8%AA%E4%BD%9C%E4%B8%9A"><span class="toc-text">布置一个作业</span></a></li></ol></li><li class="toc-item toc-level-2"><a class="toc-link" href="#%E6%B5%AE%E5%8A%A8%E7%9A%84%E6%B8%85%E9%99%A4"><span class="toc-text">浮动的清除</span></a><ol class="toc-child"><li class="toc-item toc-level-3"><a class="toc-link" href="#%E5%89%8D%E8%A8%80"><span class="toc-text">前言</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#%E6%96%B9%E6%B3%951%EF%BC%9A%E7%BB%99%E6%B5%AE%E5%8A%A8%E5%85%83%E7%B4%A0%E7%9A%84%E7%A5%96%E5%85%88%E5%85%83%E7%B4%A0%E5%8A%A0%E9%AB%98%E5%BA%A6"><span class="toc-text">方法1：给浮动元素的祖先元素加高度</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#%E6%96%B9%E6%B3%952%EF%BC%9Aclear-both"><span class="toc-text">方法2：clear:both;</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#%E6%96%B9%E6%B3%953%EF%BC%9A%E9%9A%94%E5%A2%99%E6%B3%95"><span class="toc-text">方法3：隔墙法</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#%E6%B8%85%E9%99%A4%E6%B5%AE%E5%8A%A8%E6%96%B9%E6%B3%954%EF%BC%9Aoverflow-hidden"><span class="toc-text">清除浮动方法4：overflow:hidden;</span></a></li></ol></li><li class="toc-item toc-level-2"><a class="toc-link" href="#%E6%B5%AE%E5%8A%A8%E6%B8%85%E9%99%A4%E7%9A%84%E6%80%BB%E7%BB%93"><span class="toc-text">浮动清除的总结</span></a><ol class="toc-child"><li class="toc-item toc-level-3"><a class="toc-link" href="#1%E3%80%81%E5%8A%A0%E9%AB%98%E6%B3%95"><span class="toc-text">1、加高法</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#2%E3%80%81clear-both-%E6%B3%95"><span class="toc-text">2、clear:both;法</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#3%E3%80%81%E9%9A%94%E5%A2%99%E6%B3%95"><span class="toc-text">3、隔墙法</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#4%E3%80%81overflow-hidden"><span class="toc-text">4、overflow:hidden;</span></a></li></ol></li><li class="toc-item toc-level-2"><a class="toc-link" href="#%E6%B5%8F%E8%A7%88%E5%99%A8%E7%9A%84%E5%85%BC%E5%AE%B9%E6%80%A7%E9%97%AE%E9%A2%98"><span class="toc-text">浏览器的兼容性问题</span></a><ol class="toc-child"><li class="toc-item toc-level-3"><a class="toc-link" href="#%E5%85%BC%E5%AE%B9%E6%80%A71%EF%BC%88%E5%BE%AE%E5%9E%8B%E7%9B%92%E5%AD%90%EF%BC%89"><span class="toc-text">兼容性1（微型盒子）</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#%E5%85%BC%E5%AE%B9%E6%80%A72"><span class="toc-text">兼容性2</span></a></li></ol></li><li class="toc-item toc-level-2"><a class="toc-link" href="#margin%E7%9B%B8%E5%85%B3"><span class="toc-text">margin相关</span></a><ol class="toc-child"><li class="toc-item toc-level-3"><a class="toc-link" href="#margin%E5%A1%8C%E9%99%B7-x2F-margin%E9%87%8D%E5%8F%A0"><span class="toc-text">margin塌陷&#x2F;margin重叠</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#%E7%9B%92%E5%AD%90%E5%B1%85%E4%B8%ADmargin-0-auto"><span class="toc-text">盒子居中margin:0 auto;</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#%E5%96%84%E4%BA%8E%E4%BD%BF%E7%94%A8%E7%88%B6%E4%BA%B2%E7%9A%84padding%EF%BC%8C%E8%80%8C%E4%B8%8D%E6%98%AF%E5%84%BF%E5%AD%90%E7%9A%84margin"><span class="toc-text">善于使用父亲的padding，而不是儿子的margin</span></a></li></ol></li><li class="toc-item toc-level-2"><a class="toc-link" href="#%E5%85%B3%E4%BA%8Emargin%E7%9A%84IE6%E5%85%BC%E5%AE%B9%E9%97%AE%E9%A2%98"><span class="toc-text">关于margin的IE6兼容问题</span></a><ol class="toc-child"><li class="toc-item toc-level-3"><a class="toc-link" href="#IE6%E7%9A%84%E5%8F%8C%E5%80%8Dmargin%E7%9A%84bug%EF%BC%9A"><span class="toc-text">IE6的双倍margin的bug：</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#IE6%E7%9A%843px-bug"><span class="toc-text">IE6的3px bug</span></a></li></ol></li><li class="toc-item toc-level-2"><a class="toc-link" href="#Fireworks%E5%92%8Cothers"><span class="toc-text">Fireworks和others</span></a><ol class="toc-child"><li class="toc-item toc-level-3"><a class="toc-link" href="#Fireworks"><span class="toc-text">Fireworks</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#others"><span class="toc-text">others</span></a></li></ol></li><li class="toc-item toc-level-2"><a class="toc-link" href="#%E6%88%91%E7%9A%84%E5%85%AC%E4%BC%97%E5%8F%B7"><span class="toc-text">我的公众号</span></a></li></ol>
      </div>
    </article>
  
  
  <article class="card card-content categories-widget">
    <div class="categories-card">
  <div class="categories-header">
    <i 
      class="iconfont icon-fenlei" 
      style="padding-right: 2px;">
    </i>分类
  </div>
  <div class="categories-list">
    
      <a href="/categories/%E5%89%8D%E7%AB%AF%E5%B7%A5%E5%85%B7/">
        <div class="categories-list-item">
          前端工具
          <span class="categories-list-item-badge">13</span>
        </div>
      </a>
    
      <a href="/categories/CSS%E5%9F%BA%E7%A1%80/">
        <div class="categories-list-item">
          CSS基础
          <span class="categories-list-item-badge">18</span>
        </div>
      </a>
    
      <a href="/categories/CSS%E8%BF%9B%E9%98%B6/">
        <div class="categories-list-item">
          CSS进阶
          <span class="categories-list-item-badge">9</span>
        </div>
      </a>
    
      <a href="/categories/HTML/">
        <div class="categories-list-item">
          HTML
          <span class="categories-list-item-badge">12</span>
        </div>
      </a>
    
      <a href="/categories/JS%E5%9F%BA%E7%A1%80/">
        <div class="categories-list-item">
          JS基础
          <span class="categories-list-item-badge">56</span>
        </div>
      </a>
    
      <a href="/categories/JS%E5%9F%BA%E7%A1%80ES6/">
        <div class="categories-list-item">
          JS基础ES6
          <span class="categories-list-item-badge">10</span>
        </div>
      </a>
    
      <a href="/categories/JS%E5%9F%BA%E7%A1%80%E5%BC%82%E6%AD%A5/">
        <div class="categories-list-item">
          JS基础异步
          <span class="categories-list-item-badge">11</span>
        </div>
      </a>
    
      <a href="/categories/JS%E8%BF%9B%E9%98%B6/">
        <div class="categories-list-item">
          JS进阶
          <span class="categories-list-item-badge">11</span>
        </div>
      </a>
    
      <a href="/categories/%E7%A7%BB%E5%8A%A8web/">
        <div class="categories-list-item">
          移动web
          <span class="categories-list-item-badge">3</span>
        </div>
      </a>
    
      <a href="/categories/CSS%E5%92%8CDOM/">
        <div class="categories-list-item">
          CSS和DOM
          <span class="categories-list-item-badge">6</span>
        </div>
      </a>
    
      <a href="/categories/Node/">
        <div class="categories-list-item">
          Node
          <span class="categories-list-item-badge">16</span>
        </div>
      </a>
    
      <a href="/categories/Vue%E5%9F%BA%E7%A1%80/">
        <div class="categories-list-item">
          Vue基础
          <span class="categories-list-item-badge">15</span>
        </div>
      </a>
    
      <a href="/categories/%E6%80%A7%E8%83%BD%E4%BC%98%E5%8C%96/">
        <div class="categories-list-item">
          性能优化
          <span class="categories-list-item-badge">7</span>
        </div>
      </a>
    
      <a href="/categories/%E5%89%8D%E7%AB%AF%E5%B7%A5%E7%A8%8B%E5%8C%96/">
        <div class="categories-list-item">
          前端工程化
          <span class="categories-list-item-badge">8</span>
        </div>
      </a>
    
  </div>
</div>
  </article>
  
  <article class="card card-content tags-widget">
    <div class="tags-card">
  <div class="tags-header">
    <i 
      class="iconfont icon-biaoqian" 
      style="padding-right: 2px;">
    </i>热门标签
  </div>
  <div class="tags-list">
    
      <a 
        href="/tags/JS%E5%9F%BA%E7%A1%80/" 
        title="JS基础">
        <div class="tags-list-item">JS基础</div>
      </a>
    
      <a 
        href="/tags/CSS%E5%9F%BA%E7%A1%80/" 
        title="CSS基础">
        <div class="tags-list-item">CSS基础</div>
      </a>
    
      <a 
        href="/tags/Node/" 
        title="Node">
        <div class="tags-list-item">Node</div>
      </a>
    
      <a 
        href="/tags/Vue%E5%9F%BA%E7%A1%80/" 
        title="Vue基础">
        <div class="tags-list-item">Vue基础</div>
      </a>
    
      <a 
        href="/tags/%E5%89%8D%E7%AB%AF%E5%B7%A5%E5%85%B7/" 
        title="前端工具">
        <div class="tags-list-item">前端工具</div>
      </a>
    
      <a 
        href="/tags/HTML/" 
        title="HTML">
        <div class="tags-list-item">HTML</div>
      </a>
    
      <a 
        href="/tags/JS%E8%BF%9B%E9%98%B6/" 
        title="JS进阶">
        <div class="tags-list-item">JS进阶</div>
      </a>
    
      <a 
        href="/tags/JS%E5%9F%BA%E7%A1%80%E5%BC%82%E6%AD%A5/" 
        title="JS基础异步">
        <div class="tags-list-item">JS基础异步</div>
      </a>
    
      <a 
        href="/tags/JS%E5%9F%BA%E7%A1%80ES6/" 
        title="JS基础ES6">
        <div class="tags-list-item">JS基础ES6</div>
      </a>
    
      <a 
        href="/tags/CSS%E8%BF%9B%E9%98%B6/" 
        title="CSS进阶">
        <div class="tags-list-item">CSS进阶</div>
      </a>
    
      <a 
        href="/tags/%E5%89%8D%E7%AB%AF%E5%B7%A5%E7%A8%8B%E5%8C%96/" 
        title="前端工程化">
        <div class="tags-list-item">前端工程化</div>
      </a>
    
      <a 
        href="/tags/%E6%80%A7%E8%83%BD%E4%BC%98%E5%8C%96/" 
        title="性能优化">
        <div class="tags-list-item">性能优化</div>
      </a>
    
      <a 
        href="/tags/CSS%E5%92%8CDOM/" 
        title="CSS和DOM">
        <div class="tags-list-item">CSS和DOM</div>
      </a>
    
      <a 
        href="/tags/%E7%A7%BB%E5%8A%A8web/" 
        title="移动web">
        <div class="tags-list-item">移动web</div>
      </a>
    
  </div>
</div>
  </article>
  
  
</div>
            </aside>
            <main class="main-column">
              
<article class="card card-content">
  <header>
    <h1 class="post-title">
      07-浮动
    </h1>
  </header>
  <div class="post-meta post-show-meta">
    <time datetime="2001-12-31T16:00:00.000Z">
      <i 
        class="iconfont icon-calendar" 
        style="margin-right: 2px;">
      </i>
      <span>2002-01-01</span>
    </time>
    
      <span class="dot"></span>
      
        <a 
          href="/categories/CSS%E5%9F%BA%E7%A1%80/" 
          class="post-meta-link">
          CSS基础
        </a>
      
    
    
      <span class="dot"></span>
      <span>6.5k 字</span>
    
  </div>
  
    <div 
      class="post-meta post-show-meta" 
      style="margin-top: -10px;">
      <div style="display: flex; align-items: center;">
        <i 
          class="iconfont icon-biaoqian" 
          style="margin-right: 2px; font-size: 1.15rem;">
        </i>
        
          
          <a 
            href="/tags/CSS%E5%9F%BA%E7%A1%80/" 
            class="post-meta-link">
            CSS基础
          </a>
        
      </div>
    </div>
  
  </header>
  <div 
    id="section" 
    class="post-content">
    <p><ArticleTopAd></ArticleTopAd></p>
<h2 id="文本主要内容"><a href="#文本主要内容" class="headerlink" title="文本主要内容"></a>文本主要内容</h2><ul>
<li>标准文档流<ul>
<li>标准文档流的特性</li>
<li>行内元素和块级元素</li>
<li>行内元素和块级元素的相互转换</li>
</ul>
</li>
<li>浮动的性质</li>
<li>浮动的清除</li>
<li>浏览器的兼容性问题</li>
<li>浮动中margin相关</li>
<li>关于margin的IE6兼容问题</li>
</ul>
<h2 id="标准文档流"><a href="#标准文档流" class="headerlink" title="标准文档流"></a>标准文档流</h2><p>宏观地讲，我们的web页面和photoshop等设计软件有本质的区别：web页面的制作，是个“流”，必须从上而下，像“织毛衣”。而设计软件，想往哪里画个东西，都能画。</p>
<h3 id="标准文档流的特性"><a href="#标准文档流的特性" class="headerlink" title="标准文档流的特性"></a>标准文档流的特性</h3><p><strong>（1）空白折叠现象：</strong></p>
<p>无论多少个空格、换行、tab，都会折叠为一个空格。</p>
<p>比如，如果我们想让img标签之间没有空隙，必须紧密连接：</p>
<pre class="highlight"><span class="line">&lt;img src=&quot;images/0.jpg&quot; /&gt;&lt;img src=&quot;images/1.jpg&quot; /&gt;&lt;img src=&quot;images/2.jpg&quot; /&gt;</span><br></pre>


<p><strong>（2）高矮不齐，底边对齐：</strong></p>
<p>举例如下：</p>
<p><img  srcset="data:image/svg+xml,%3Csvg%20xmlns='http://www.w3.org/2000/svg'%20viewBox='0%200%20300%20300'%3E%3C/svg%3E" data-src="http://img.smyhvae.com/20170729_1508_2.png" class="lozad post-image"src="http://img.smyhvae.com/20170729_1508_2.png"></p>
<p><strong>（3）自动换行，一行写不满，换行写。</strong></p>
<h3 id="行内元素和块级元素"><a href="#行内元素和块级元素" class="headerlink" title="行内元素和块级元素"></a>行内元素和块级元素</h3><p>学习的初期，我们就要知道，标准文档流等级森严。标签分为两种等级：</p>
<ul>
<li>行内元素</li>
<li>块级元素</li>
</ul>
<p>我们可以举一个例子，看看块级元素和行内元素的区别：</p>
<p><img  srcset="data:image/svg+xml,%3Csvg%20xmlns='http://www.w3.org/2000/svg'%20viewBox='0%200%20300%20300'%3E%3C/svg%3E" data-src="http://img.smyhvae.com/20170729_1529_2.png" class="lozad post-image"src="http://img.smyhvae.com/20170729_1529_2.png"></p>
<p>上图中可以看到，<code>h1</code>标签是块级元素，占据了整行，<code>span</code>标签是行内元素，只占据内容这一部分。</p>
<p>现在我们尝试给两个标签设置宽高。效果如下：</p>
<p><img  srcset="data:image/svg+xml,%3Csvg%20xmlns='http://www.w3.org/2000/svg'%20viewBox='0%200%20300%20300'%3E%3C/svg%3E" data-src="http://img.smyhvae.com/20170729_1532_2.png" class="lozad post-image"src="http://img.smyhvae.com/20170729_1532_2.png"></p>
<p>上图中，我们尝试给两个标签设置宽高，但发现，宽高属性只对块级元素<code>h1</code>生效。于是我们可以做出如下总结。</p>
<p><strong>行内元素和块级元素的区别：</strong>（非常重要）</p>
<p>行内元素：</p>
<ul>
<li>与其他行内元素并排；</li>
<li>不能设置宽、高。默认的宽度，就是文字的宽度。</li>
</ul>
<p>块级元素：</p>
<ul>
<li>霸占一行，不能与其他任何元素并列；</li>
<li>能接受宽、高。如果不设置宽度，那么宽度将默认变为父亲的100%。</li>
</ul>
<p><strong>行内元素和块级元素的分类：</strong></p>
<p>在以前的HTML知识中，我们已经将标签分过类，当时分为了：文本级、容器级。</p>
<p>从HTML的角度来讲，标签分为：</p>
<ul>
<li>文本级标签：p、span、a、b、i、u、em。</li>
<li>容器级标签：div、h系列、li、dt、dd。</li>
</ul>
<blockquote>
<p>PS：为甚么说p是文本级标签呢？因为p里面只能放文字&amp;图片&amp;表单元素，p里面不能放h和ul，p里面也不能放p。</p>
</blockquote>
<p>现在，从CSS的角度讲，CSS的分类和上面的很像，就p不一样：</p>
<ul>
<li><p>行内元素：除了p之外，所有的文本级标签，都是行内元素。p是个文本级，但是是个块级元素。</p>
</li>
<li><p>块级元素：所有的容器级标签都是块级元素，还有p标签。</p>
</li>
</ul>
<p>我们把上面的分类画一个图，即可一目了然：</p>
<p><img  srcset="data:image/svg+xml,%3Csvg%20xmlns='http://www.w3.org/2000/svg'%20viewBox='0%200%20300%20300'%3E%3C/svg%3E" data-src="http://img.smyhvae.com/20170729_1545.png" class="lozad post-image"src="http://img.smyhvae.com/20170729_1545.png"></p>
<h3 id="行内元素和块级元素的相互转换"><a href="#行内元素和块级元素的相互转换" class="headerlink" title="行内元素和块级元素的相互转换"></a>行内元素和块级元素的相互转换</h3><p>我们可以通过<code>display</code>属性将块级元素和行内元素进行相互转换。display即“显示模式”。</p>
<h4 id="块级元素可以转换为行内元素："><a href="#块级元素可以转换为行内元素：" class="headerlink" title="块级元素可以转换为行内元素："></a>块级元素可以转换为行内元素：</h4><p>一旦，给一个块级元素（比如div）设置：</p>
<pre class="highlight"><span class="line">display: inline;</span><br></pre>

<p>那么，这个标签将立即变为行内元素，此时它和一个span无异。inline就是“行内”。也就是说：</p>
<ul>
<li>此时这个div不能设置宽度、高度；</li>
<li>此时这个div可以和别人并排了。</li>
</ul>
<p>举例如下：</p>
<p><img  srcset="data:image/svg+xml,%3Csvg%20xmlns='http://www.w3.org/2000/svg'%20viewBox='0%200%20300%20300'%3E%3C/svg%3E" data-src="http://img.smyhvae.com/20170729_1629.png" class="lozad post-image"src="http://img.smyhvae.com/20170729_1629.png"></p>
<h4 id="行内元素转换为块级元素："><a href="#行内元素转换为块级元素：" class="headerlink" title="行内元素转换为块级元素："></a>行内元素转换为块级元素：</h4><p>同样的道理，一旦给一个行内元素（比如span）设置：</p>
<pre class="highlight"><span class="line">display: block;</span><br></pre>

<p>那么，这个标签将立即变为块级元素，此时它和一个div无异。block”是“块”的意思。也就是说：</p>
<ul>
<li>此时这个span能够设置宽度、高度</li>
<li>此时这个span必须霸占一行了，别人无法和他并排</li>
<li>如果不设置宽度，将撑满父亲</li>
</ul>
<p>举例如下：</p>
<p><img  srcset="data:image/svg+xml,%3Csvg%20xmlns='http://www.w3.org/2000/svg'%20viewBox='0%200%20300%20300'%3E%3C/svg%3E" data-src="http://img.smyhvae.com/20170729_1638.png" class="lozad post-image"src="http://img.smyhvae.com/20170729_1638.png"></p>
<p>标准流里面的限制非常多，导致很多页面效果无法实现。如果我们现在就要并排、并且就要设置宽高，那该怎么办呢？办法是：移民！<strong>脱离标准流</strong>！</p>
<p>css中一共有三种手段，使一个元素脱离标准文档流：</p>
<ul>
<li>（1）浮动</li>
<li>（2）绝对定位</li>
<li>（3）固定定位</li>
</ul>
<p>这便引出我们今天要讲的内容：浮动。</p>
<h2 id="浮动的性质"><a href="#浮动的性质" class="headerlink" title="浮动的性质"></a>浮动的性质</h2><blockquote>
<p>浮动是css里面布局用的最多的属性。</p>
</blockquote>
<p>现在有两个div，分别设置宽高。我们知道，它们的效果如下：</p>
<p><img  srcset="data:image/svg+xml,%3Csvg%20xmlns='http://www.w3.org/2000/svg'%20viewBox='0%200%20300%20300'%3E%3C/svg%3E" data-src="http://img.smyhvae.com/20170729_1722.png" class="lozad post-image"src="http://img.smyhvae.com/20170729_1722.png"></p>
<p>此时，如果给这两个div增加一个浮动属性，比如<code>float: left;</code>，效果如下：</p>
<p><img  srcset="data:image/svg+xml,%3Csvg%20xmlns='http://www.w3.org/2000/svg'%20viewBox='0%200%20300%20300'%3E%3C/svg%3E" data-src="http://img.smyhvae.com/20170729_1723.png" class="lozad post-image"src="http://img.smyhvae.com/20170729_1723.png"></p>
<p>这就达到了浮动的效果。此时，两个元素并排了，并且两个元素都能够设置宽度、高度了（这在上一段的标准流中，不能实现）。</p>
<p>浮动想学好，一定要知道三个性质。接下来讲一讲。</p>
<h3 id="性质1：浮动的元素脱标"><a href="#性质1：浮动的元素脱标" class="headerlink" title="性质1：浮动的元素脱标"></a>性质1：浮动的元素脱标</h3><p>脱标即脱离标准流。我们来看几个例子。</p>
<p>证明1：</p>
<p><img  srcset="data:image/svg+xml,%3Csvg%20xmlns='http://www.w3.org/2000/svg'%20viewBox='0%200%20300%20300'%3E%3C/svg%3E" data-src="http://img.smyhvae.com/20170729_2028.png" class="lozad post-image"src="http://img.smyhvae.com/20170729_2028.png"></p>
<p>上图中，在默认情况下，两个div标签是上下进行排列的。现在由于float属性让上图中的第一个<code>&lt;div&gt;</code>标签出现了浮动，于是这个标签在另外一个层面上进行排列。而第二个<code>&lt;div&gt;</code>还在自己的层面上遵从标准流进行排列。</p>
<p>证明2：</p>
<p><img  srcset="data:image/svg+xml,%3Csvg%20xmlns='http://www.w3.org/2000/svg'%20viewBox='0%200%20300%20300'%3E%3C/svg%3E" data-src="http://img.smyhvae.com/20180111_2320.png" class="lozad post-image"src="http://img.smyhvae.com/20180111_2320.png"></p>
<p>上图中，span标签在标准流中，是不能设置宽高的（因为是行内元素）。但是，一旦设置为浮动之后，即使不转成块级元素，也能够设置宽高了。</p>
<p>所以能够证明一件事：<strong>一旦一个元素浮动了，那么，将能够并排了，并且能够设置宽高了。无论它原来是个div还是个span。</strong>所有标签，浮动之后，已经不区分行内、块级了。</p>
<h3 id="性质2：浮动的元素互相贴靠"><a href="#性质2：浮动的元素互相贴靠" class="headerlink" title="性质2：浮动的元素互相贴靠"></a>性质2：浮动的元素互相贴靠</h3><p>我们来看一个例子就明白了。</p>
<p>我们给三个div均设置了<code>float: left;</code>属性之后，然后设置宽高。当改变浏览器窗口大小时，可以看到div的贴靠效果：</p>
<p><img  srcset="data:image/svg+xml,%3Csvg%20xmlns='http://www.w3.org/2000/svg'%20viewBox='0%200%20300%20300'%3E%3C/svg%3E" data-src="http://img.smyhvae.com/20170730_1910.gif" class="lozad post-image"src="http://img.smyhvae.com/20170730_1910.gif"></p>
<p>上图显示，3号如果有足够空间，那么就会靠着2号。如果没有足够的空间，那么会靠着1号大哥。<br>如果没有足够的空间靠着1号大哥，3号自己去贴左墙。</p>
<p>不过3号自己去贴墙的时候，注意：</p>
<p><img  srcset="data:image/svg+xml,%3Csvg%20xmlns='http://www.w3.org/2000/svg'%20viewBox='0%200%20300%20300'%3E%3C/svg%3E" data-src="http://img.smyhvae.com/20170730_1928.gif" class="lozad post-image"src="http://img.smyhvae.com/20170730_1928.gif"></p>
<p>上图显示，3号贴左墙的时候，并不会往1号里面挤。</p>
<p>同样，float还有一个属性值是<code>right</code>，这个和属性值<code>left</code>是对称的。</p>
<h3 id="性质3：浮动的元素有“字围”效果"><a href="#性质3：浮动的元素有“字围”效果" class="headerlink" title="性质3：浮动的元素有“字围”效果"></a>性质3：浮动的元素有“字围”效果</h3><p>来看一张图就明白了。我们让div浮动，p不浮动。</p>
<p><img  srcset="data:image/svg+xml,%3Csvg%20xmlns='http://www.w3.org/2000/svg'%20viewBox='0%200%20300%20300'%3E%3C/svg%3E" data-src="http://img.smyhvae.com/20170730_2005.png" class="lozad post-image"src="http://img.smyhvae.com/20170730_2005.png"></p>
<p>上图中，我们发现：<strong>div挡住了p，但不会挡住p中的文字</strong>，形成“字围”效果。</p>
<p>总结：<strong>标准流中的文字不会被浮动的盒子遮挡住</strong>。（文字就像水一样）</p>
<p>关于浮动我们要强调一点，浮动这个东西，为避免混乱，我们在初期一定要遵循一个原则：<strong>永远不是一个东西单独浮动，浮动都是一起浮动，要浮动，大家都浮动。</strong></p>
<h3 id="性质4：收缩"><a href="#性质4：收缩" class="headerlink" title="性质4：收缩"></a>性质4：收缩</h3><p>收缩：一个浮动的元素，如果没有设置width，那么将自动收缩为内容的宽度（这点非常像行内元素）。</p>
<p>举例如下：</p>
<p><img  srcset="data:image/svg+xml,%3Csvg%20xmlns='http://www.w3.org/2000/svg'%20viewBox='0%200%20300%20300'%3E%3C/svg%3E" data-src="http://img.smyhvae.com/20170801_1720.png" class="lozad post-image"src="http://img.smyhvae.com/20170801_1720.png"></p>
<p>上图中，div本身是块级元素，如果不设置width，它会单独霸占整行；但是，设置div浮动后，它会收缩</p>
<h3 id="浮动的补充（做网站时注意）"><a href="#浮动的补充（做网站时注意）" class="headerlink" title="浮动的补充（做网站时注意）"></a>浮动的补充（做网站时注意）</h3><p><img  srcset="data:image/svg+xml,%3Csvg%20xmlns='http://www.w3.org/2000/svg'%20viewBox='0%200%20300%20300'%3E%3C/svg%3E" data-src="http://img.smyhvae.com/20170731_2248.png" class="lozad post-image"src="http://img.smyhvae.com/20170731_2248.png"></p>
<p>上图所示，将para1和para2设置为浮动，它们是div的儿子。此时para1+para2的宽度小于div的宽度。效果如上图所示。可如果设置para1+para2的宽度大于div的宽度，我们会发现，para2掉下来了：</p>
<p><img  srcset="data:image/svg+xml,%3Csvg%20xmlns='http://www.w3.org/2000/svg'%20viewBox='0%200%20300%20300'%3E%3C/svg%3E" data-src="http://img.smyhvae.com/20170731_2252_2.png" class="lozad post-image"src="http://img.smyhvae.com/20170731_2252_2.png"></p>
<h3 id="布置一个作业"><a href="#布置一个作业" class="headerlink" title="布置一个作业"></a>布置一个作业</h3><p>布置一个作业，要求实现下面的效果：</p>
<p><img  srcset="data:image/svg+xml,%3Csvg%20xmlns='http://www.w3.org/2000/svg'%20viewBox='0%200%20300%20300'%3E%3C/svg%3E" data-src="http://img.smyhvae.com/20170801_0858.png" class="lozad post-image"src="http://img.smyhvae.com/20170801_0858.png"></p>
<p>为实现上方效果，代码如下：</p>
<pre class="highlight"><span class="line"><span class="meta">&lt;!DOCTYPE <span class="keyword">html</span> <span class="keyword">PUBLIC</span> <span class="string">&quot;-//W3C//DTD XHTML 1.0 Transitional//EN&quot;</span> <span class="string">&quot;http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd&quot;</span>&gt;</span></span><br><span class="line"><span class="tag">&lt;<span class="name">html</span> <span class="attr">xmlns</span>=<span class="string">&quot;http://www.w3.org/1999/xhtml&quot;</span> <span class="attr">xml:lang</span>=<span class="string">&quot;en&quot;</span>&gt;</span></span><br><span class="line"><span class="tag">&lt;<span class="name">head</span>&gt;</span></span><br><span class="line">	<span class="tag">&lt;<span class="name">meta</span> <span class="attr">http-equiv</span>=<span class="string">&quot;Content-Type&quot;</span> <span class="attr">content</span>=<span class="string">&quot;text/html;charset=UTF-8&quot;</span>&gt;</span></span><br><span class="line">	<span class="tag">&lt;<span class="name">title</span>&gt;</span>Document<span class="tag">&lt;/<span class="name">title</span>&gt;</span></span><br><span class="line">	<span class="tag">&lt;<span class="name">style</span> <span class="attr">type</span>=<span class="string">&quot;text/css&quot;</span>&gt;</span><span class="language-css"></span></span><br><span class="line"><span class="language-css">		*&#123;</span></span><br><span class="line"><span class="language-css">			<span class="attribute">margin</span>: <span class="number">0</span>;</span></span><br><span class="line"><span class="language-css">			<span class="attribute">padding</span>: <span class="number">0</span>;</span></span><br><span class="line"><span class="language-css">		&#125;</span></span><br><span class="line"><span class="language-css">		<span class="selector-class">.header</span>&#123;</span></span><br><span class="line"><span class="language-css">			<span class="attribute">width</span>: <span class="number">970px</span>;</span></span><br><span class="line"><span class="language-css">			<span class="attribute">height</span>: <span class="number">103px</span>;</span></span><br><span class="line"><span class="language-css">			<span class="comment">/*居中。这个语句的意思是：居中：*/</span></span></span><br><span class="line"><span class="language-css">			<span class="attribute">margin</span>: <span class="number">0</span> auto;</span></span><br><span class="line"><span class="language-css">		&#125;</span></span><br><span class="line"><span class="language-css">		<span class="selector-class">.header</span> <span class="selector-class">.logo</span>&#123;</span></span><br><span class="line"><span class="language-css">			<span class="attribute">float</span>: left;</span></span><br><span class="line"><span class="language-css">			<span class="attribute">width</span>: <span class="number">277px</span>;</span></span><br><span class="line"><span class="language-css">			<span class="attribute">height</span>: <span class="number">103px</span>;</span></span><br><span class="line"><span class="language-css">			<span class="attribute">background-color</span>: red;</span></span><br><span class="line"><span class="language-css">		&#125;</span></span><br><span class="line"><span class="language-css">		<span class="selector-class">.header</span> <span class="selector-class">.language</span>&#123;</span></span><br><span class="line"><span class="language-css">			<span class="attribute">float</span>: right;</span></span><br><span class="line"><span class="language-css">			<span class="attribute">width</span>: <span class="number">137px</span>;</span></span><br><span class="line"><span class="language-css">			<span class="attribute">height</span>: <span class="number">49px</span>;</span></span><br><span class="line"><span class="language-css">			<span class="attribute">background-color</span>: green;</span></span><br><span class="line"><span class="language-css">			<span class="attribute">margin-bottom</span>: <span class="number">8px</span>;</span></span><br><span class="line"><span class="language-css">		&#125;</span></span><br><span class="line"><span class="language-css">		<span class="selector-class">.header</span> <span class="selector-class">.nav</span>&#123;</span></span><br><span class="line"><span class="language-css">			<span class="attribute">float</span>: right;</span></span><br><span class="line"><span class="language-css">			<span class="attribute">width</span>: <span class="number">679px</span>;</span></span><br><span class="line"><span class="language-css">			<span class="attribute">height</span>: <span class="number">46px</span>;</span></span><br><span class="line"><span class="language-css">			<span class="attribute">background-color</span>: green;</span></span><br><span class="line"><span class="language-css">		&#125;</span></span><br><span class="line"><span class="language-css"></span></span><br><span class="line"><span class="language-css">		<span class="selector-class">.content</span>&#123;</span></span><br><span class="line"><span class="language-css">			<span class="attribute">width</span>: <span class="number">970px</span>;</span></span><br><span class="line"><span class="language-css">			<span class="attribute">height</span>: <span class="number">435px</span>;</span></span><br><span class="line"><span class="language-css">			<span class="comment">/*居中，这个语句今天没讲，你照抄，就是居中：*/</span></span></span><br><span class="line"><span class="language-css">			<span class="attribute">margin</span>: <span class="number">0</span> auto;</span></span><br><span class="line"><span class="language-css">			<span class="attribute">margin-top</span>: <span class="number">10px</span>;</span></span><br><span class="line"><span class="language-css">		&#125;</span></span><br><span class="line"><span class="language-css">		<span class="selector-class">.content</span> <span class="selector-class">.banner</span>&#123;</span></span><br><span class="line"><span class="language-css">			<span class="attribute">float</span>: left;</span></span><br><span class="line"><span class="language-css">			<span class="attribute">width</span>: <span class="number">310px</span>;</span></span><br><span class="line"><span class="language-css">			<span class="attribute">height</span>: <span class="number">435px</span>;</span></span><br><span class="line"><span class="language-css">			<span class="attribute">background-color</span>: gold;</span></span><br><span class="line"><span class="language-css">			<span class="attribute">margin-right</span>: <span class="number">10px</span>;</span></span><br><span class="line"><span class="language-css">		&#125;</span></span><br><span class="line"><span class="language-css">		<span class="selector-class">.content</span> <span class="selector-class">.rightPart</span>&#123;</span></span><br><span class="line"><span class="language-css">			<span class="attribute">float</span>: left;</span></span><br><span class="line"><span class="language-css">			<span class="attribute">width</span>: <span class="number">650px</span>;</span></span><br><span class="line"><span class="language-css">			<span class="attribute">height</span>: <span class="number">435px</span>;</span></span><br><span class="line"><span class="language-css">		&#125;</span></span><br><span class="line"><span class="language-css">		<span class="selector-class">.content</span> <span class="selector-class">.rightPart</span> <span class="selector-class">.main</span>&#123;</span></span><br><span class="line"><span class="language-css">			<span class="attribute">width</span>: <span class="number">650px</span>;</span></span><br><span class="line"><span class="language-css">			<span class="attribute">height</span>: <span class="number">400px</span>;</span></span><br><span class="line"><span class="language-css">			<span class="attribute">margin-bottom</span>: <span class="number">10px</span>;</span></span><br><span class="line"><span class="language-css">		&#125;</span></span><br><span class="line"><span class="language-css">		<span class="selector-class">.content</span> <span class="selector-class">.rightPart</span> <span class="selector-class">.links</span>&#123;</span></span><br><span class="line"><span class="language-css">			<span class="attribute">width</span>: <span class="number">650px</span>;</span></span><br><span class="line"><span class="language-css">			<span class="attribute">height</span>: <span class="number">25px</span>;</span></span><br><span class="line"><span class="language-css">			<span class="attribute">background-color</span>: blue;</span></span><br><span class="line"><span class="language-css">		&#125;</span></span><br><span class="line"><span class="language-css">		<span class="selector-class">.content</span> <span class="selector-class">.rightPart</span> <span class="selector-class">.main</span> <span class="selector-class">.news</span>&#123;</span></span><br><span class="line"><span class="language-css">			<span class="attribute">float</span>: left;</span></span><br><span class="line"><span class="language-css">			<span class="attribute">width</span>: <span class="number">450px</span>;</span></span><br><span class="line"><span class="language-css">			<span class="attribute">height</span>: <span class="number">400px</span>;</span></span><br><span class="line"><span class="language-css">		&#125;</span></span><br><span class="line"><span class="language-css">		<span class="selector-class">.content</span> <span class="selector-class">.rightPart</span> <span class="selector-class">.main</span> <span class="selector-class">.hotpic</span>&#123;</span></span><br><span class="line"><span class="language-css">			<span class="attribute">float</span>: left;</span></span><br><span class="line"><span class="language-css">			<span class="attribute">width</span>: <span class="number">190px</span>;</span></span><br><span class="line"><span class="language-css">			<span class="attribute">height</span>: <span class="number">400px</span>;</span></span><br><span class="line"><span class="language-css">			<span class="attribute">background-color</span>: purple;</span></span><br><span class="line"><span class="language-css">			<span class="attribute">margin-left</span>: <span class="number">10px</span>;</span></span><br><span class="line"><span class="language-css">		&#125;</span></span><br><span class="line"><span class="language-css">		<span class="selector-class">.content</span> <span class="selector-class">.rightPart</span> <span class="selector-class">.main</span> <span class="selector-class">.news</span> <span class="selector-class">.news1</span>&#123;</span></span><br><span class="line"><span class="language-css">			<span class="attribute">width</span>: <span class="number">450px</span>;</span></span><br><span class="line"><span class="language-css">			<span class="attribute">height</span>: <span class="number">240px</span>;</span></span><br><span class="line"><span class="language-css">			<span class="attribute">background-color</span>: skyblue;</span></span><br><span class="line"><span class="language-css">			<span class="attribute">margin-bottom</span>: <span class="number">10px</span>;</span></span><br><span class="line"><span class="language-css">		&#125;</span></span><br><span class="line"><span class="language-css">		<span class="selector-class">.content</span> <span class="selector-class">.rightPart</span> <span class="selector-class">.main</span> <span class="selector-class">.news</span> <span class="selector-class">.news2</span>&#123;</span></span><br><span class="line"><span class="language-css">			<span class="attribute">width</span>: <span class="number">450px</span>;</span></span><br><span class="line"><span class="language-css">			<span class="attribute">height</span>: <span class="number">110px</span>;</span></span><br><span class="line"><span class="language-css">			<span class="attribute">background-color</span>: skyblue;</span></span><br><span class="line"><span class="language-css">			<span class="attribute">margin-bottom</span>: <span class="number">10px</span>;</span></span><br><span class="line"><span class="language-css">		&#125;</span></span><br><span class="line"><span class="language-css">		<span class="selector-class">.content</span> <span class="selector-class">.rightPart</span> <span class="selector-class">.main</span> <span class="selector-class">.news</span> <span class="selector-class">.news3</span>&#123;</span></span><br><span class="line"><span class="language-css">			<span class="attribute">width</span>: <span class="number">450px</span>;</span></span><br><span class="line"><span class="language-css">			<span class="attribute">height</span>: <span class="number">30px</span>;</span></span><br><span class="line"><span class="language-css">			<span class="attribute">background-color</span>: skyblue;</span></span><br><span class="line"><span class="language-css">		&#125;</span></span><br><span class="line"><span class="language-css">		<span class="selector-class">.footer</span>&#123;</span></span><br><span class="line"><span class="language-css">			<span class="attribute">width</span>: <span class="number">970px</span>;</span></span><br><span class="line"><span class="language-css">			<span class="attribute">height</span>: <span class="number">35px</span>;</span></span><br><span class="line"><span class="language-css">			<span class="attribute">background-color</span>: pink;</span></span><br><span class="line"><span class="language-css">			<span class="comment">/*没学，就是居中：*/</span></span></span><br><span class="line"><span class="language-css">			<span class="attribute">margin</span>: <span class="number">0</span> auto;</span></span><br><span class="line"><span class="language-css">			<span class="attribute">margin-top</span>: <span class="number">10px</span>;</span></span><br><span class="line"><span class="language-css">		&#125;</span></span><br><span class="line"><span class="language-css">	</span><span class="tag">&lt;/<span class="name">style</span>&gt;</span></span><br><span class="line"><span class="tag">&lt;/<span class="name">head</span>&gt;</span></span><br><span class="line"><span class="tag">&lt;<span class="name">body</span>&gt;</span></span><br><span class="line">	<span class="comment">&lt;!-- 头部 --&gt;</span></span><br><span class="line">	<span class="tag">&lt;<span class="name">div</span> <span class="attr">class</span>=<span class="string">&quot;header&quot;</span>&gt;</span></span><br><span class="line">		<span class="tag">&lt;<span class="name">div</span> <span class="attr">class</span>=<span class="string">&quot;logo&quot;</span>&gt;</span>logo<span class="tag">&lt;/<span class="name">div</span>&gt;</span></span><br><span class="line">		<span class="tag">&lt;<span class="name">div</span> <span class="attr">class</span>=<span class="string">&quot;language&quot;</span>&gt;</span>语言选择<span class="tag">&lt;/<span class="name">div</span>&gt;</span></span><br><span class="line">		<span class="tag">&lt;<span class="name">div</span> <span class="attr">class</span>=<span class="string">&quot;nav&quot;</span>&gt;</span>导航条<span class="tag">&lt;/<span class="name">div</span>&gt;</span></span><br><span class="line">	<span class="tag">&lt;/<span class="name">div</span>&gt;</span></span><br><span class="line"></span><br><span class="line">	<span class="comment">&lt;!-- 主要内容 --&gt;</span></span><br><span class="line">	<span class="tag">&lt;<span class="name">div</span> <span class="attr">class</span>=<span class="string">&quot;content&quot;</span>&gt;</span></span><br><span class="line">		<span class="tag">&lt;<span class="name">div</span> <span class="attr">class</span>=<span class="string">&quot;banner&quot;</span>&gt;</span>大广告<span class="tag">&lt;/<span class="name">div</span>&gt;</span></span><br><span class="line">		<span class="tag">&lt;<span class="name">div</span> <span class="attr">class</span>=<span class="string">&quot;rightPart&quot;</span>&gt;</span></span><br><span class="line">			<span class="tag">&lt;<span class="name">div</span> <span class="attr">class</span>=<span class="string">&quot;main&quot;</span>&gt;</span></span><br><span class="line">				<span class="tag">&lt;<span class="name">div</span> <span class="attr">class</span>=<span class="string">&quot;news&quot;</span>&gt;</span></span><br><span class="line">					<span class="tag">&lt;<span class="name">div</span> <span class="attr">class</span>=<span class="string">&quot;news1&quot;</span>&gt;</span><span class="tag">&lt;/<span class="name">div</span>&gt;</span></span><br><span class="line">					<span class="tag">&lt;<span class="name">div</span> <span class="attr">class</span>=<span class="string">&quot;news2&quot;</span>&gt;</span><span class="tag">&lt;/<span class="name">div</span>&gt;</span></span><br><span class="line">					<span class="tag">&lt;<span class="name">div</span> <span class="attr">class</span>=<span class="string">&quot;news3&quot;</span>&gt;</span><span class="tag">&lt;/<span class="name">div</span>&gt;</span></span><br><span class="line">				<span class="tag">&lt;/<span class="name">div</span>&gt;</span></span><br><span class="line">				<span class="tag">&lt;<span class="name">div</span> <span class="attr">class</span>=<span class="string">&quot;hotpic&quot;</span>&gt;</span><span class="tag">&lt;/<span class="name">div</span>&gt;</span></span><br><span class="line">			<span class="tag">&lt;/<span class="name">div</span>&gt;</span></span><br><span class="line">			<span class="tag">&lt;<span class="name">div</span> <span class="attr">class</span>=<span class="string">&quot;links&quot;</span>&gt;</span><span class="tag">&lt;/<span class="name">div</span>&gt;</span></span><br><span class="line">		<span class="tag">&lt;/<span class="name">div</span>&gt;</span></span><br><span class="line">	<span class="tag">&lt;/<span class="name">div</span>&gt;</span></span><br><span class="line"></span><br><span class="line">	<span class="comment">&lt;!-- 页尾 --&gt;</span></span><br><span class="line">	<span class="tag">&lt;<span class="name">div</span> <span class="attr">class</span>=<span class="string">&quot;footer&quot;</span>&gt;</span><span class="tag">&lt;/<span class="name">div</span>&gt;</span></span><br><span class="line"><span class="tag">&lt;/<span class="name">body</span>&gt;</span></span><br><span class="line"><span class="tag">&lt;/<span class="name">html</span>&gt;</span></span><br></pre>

<p>其实，这个页面的布局是下面这个网站：</p>
<p><img  srcset="data:image/svg+xml,%3Csvg%20xmlns='http://www.w3.org/2000/svg'%20viewBox='0%200%20300%20300'%3E%3C/svg%3E" data-src="http://img.smyhvae.com/20170801_0900.png" class="lozad post-image"src="http://img.smyhvae.com/20170801_0900.png"></p>
<h2 id="浮动的清除"><a href="#浮动的清除" class="headerlink" title="浮动的清除"></a>浮动的清除</h2><blockquote>
<p>这里所说的清除浮动，指的是清除浮动与浮动之间的影响。</p>
</blockquote>
<h3 id="前言"><a href="#前言" class="headerlink" title="前言"></a>前言</h3><p>通过上面这个例子，我们发现，此例中的网页就是通过浮动实现并排的。</p>
<p>比如说一个网页有header、content、footer这三部分。就拿content部分来举例，如果设置content的儿子为浮动，但是，这个儿子又是一个全新的标准流，于是儿子的儿子仍然在标准流里。</p>
<p>从学习浮动的第一天起，我们就要明白，浮动有开始，就要有清除。我们先来做个实验。</p>
<p>下面这个例子，有两个块级元素div，div没有任何属性，每个div里有li，效果如下：</p>
<p><img  srcset="data:image/svg+xml,%3Csvg%20xmlns='http://www.w3.org/2000/svg'%20viewBox='0%200%20300%20300'%3E%3C/svg%3E" data-src="http://img.smyhvae.com/20170801_2122.png" class="lozad post-image"src="http://img.smyhvae.com/20170801_2122.png"></p>
<p>上面这个例子很简单。可如果我们给里面的<code>&lt;li&gt;</code>标签加浮动。效果却成了下面这个样子：</p>
<p>代码如下：</p>
<pre class="highlight"><span class="line"><span class="meta">&lt;!DOCTYPE <span class="keyword">html</span>&gt;</span></span><br><span class="line"><span class="tag">&lt;<span class="name">html</span> <span class="attr">lang</span>=<span class="string">&quot;en&quot;</span>&gt;</span></span><br><span class="line"><span class="tag">&lt;<span class="name">head</span>&gt;</span></span><br><span class="line">	<span class="tag">&lt;<span class="name">meta</span> <span class="attr">charset</span>=<span class="string">&quot;UTF-8&quot;</span>&gt;</span></span><br><span class="line">	<span class="tag">&lt;<span class="name">title</span>&gt;</span>Document<span class="tag">&lt;/<span class="name">title</span>&gt;</span></span><br><span class="line">	<span class="tag">&lt;<span class="name">style</span> <span class="attr">type</span>=<span class="string">&quot;text/css&quot;</span>&gt;</span><span class="language-css"></span></span><br><span class="line"><span class="language-css">		*&#123;</span></span><br><span class="line"><span class="language-css"></span></span><br><span class="line"><span class="language-css">		&#125;</span></span><br><span class="line"><span class="language-css">		<span class="selector-tag">li</span>&#123;</span></span><br><span class="line"><span class="language-css">			<span class="attribute">float</span>: left;</span></span><br><span class="line"><span class="language-css">			<span class="attribute">width</span>: <span class="number">100px</span>;</span></span><br><span class="line"><span class="language-css">			<span class="attribute">height</span>: <span class="number">20px</span>;</span></span><br><span class="line"><span class="language-css">			<span class="attribute">background-color</span>: pink;</span></span><br><span class="line"><span class="language-css"></span></span><br><span class="line"><span class="language-css"></span></span><br><span class="line"><span class="language-css">		&#125;</span></span><br><span class="line"><span class="language-css">	</span><span class="tag">&lt;/<span class="name">style</span>&gt;</span></span><br><span class="line"><span class="tag">&lt;/<span class="name">head</span>&gt;</span></span><br><span class="line"><span class="tag">&lt;<span class="name">body</span>&gt;</span></span><br><span class="line">	<span class="tag">&lt;<span class="name">div</span> <span class="attr">class</span>=<span class="string">&quot;box1&quot;</span>&gt;</span></span><br><span class="line">		<span class="tag">&lt;<span class="name">ul</span>&gt;</span></span><br><span class="line">			<span class="tag">&lt;<span class="name">li</span>&gt;</span>生命壹号1<span class="tag">&lt;/<span class="name">li</span>&gt;</span></span><br><span class="line">			<span class="tag">&lt;<span class="name">li</span>&gt;</span>生命壹号2<span class="tag">&lt;/<span class="name">li</span>&gt;</span></span><br><span class="line">			<span class="tag">&lt;<span class="name">li</span>&gt;</span>生命壹号3<span class="tag">&lt;/<span class="name">li</span>&gt;</span></span><br><span class="line">			<span class="tag">&lt;<span class="name">li</span>&gt;</span>生命壹号4<span class="tag">&lt;/<span class="name">li</span>&gt;</span></span><br><span class="line">		<span class="tag">&lt;/<span class="name">ul</span>&gt;</span></span><br><span class="line">	<span class="tag">&lt;/<span class="name">div</span>&gt;</span></span><br><span class="line">	<span class="tag">&lt;<span class="name">div</span> <span class="attr">class</span>=<span class="string">&quot;box2&quot;</span>&gt;</span></span><br><span class="line">		<span class="tag">&lt;<span class="name">ul</span>&gt;</span></span><br><span class="line">			<span class="tag">&lt;<span class="name">li</span>&gt;</span>许嵩1<span class="tag">&lt;/<span class="name">li</span>&gt;</span></span><br><span class="line">			<span class="tag">&lt;<span class="name">li</span>&gt;</span>许嵩2<span class="tag">&lt;/<span class="name">li</span>&gt;</span></span><br><span class="line">			<span class="tag">&lt;<span class="name">li</span>&gt;</span>许嵩3<span class="tag">&lt;/<span class="name">li</span>&gt;</span></span><br><span class="line">			<span class="tag">&lt;<span class="name">li</span>&gt;</span>许嵩4<span class="tag">&lt;/<span class="name">li</span>&gt;</span></span><br><span class="line">		<span class="tag">&lt;/<span class="name">ul</span>&gt;</span></span><br><span class="line">	<span class="tag">&lt;/<span class="name">div</span>&gt;</span></span><br><span class="line"><span class="tag">&lt;/<span class="name">body</span>&gt;</span></span><br><span class="line"><span class="tag">&lt;/<span class="name">html</span>&gt;</span></span><br></pre>

<p>效果如下：</p>
<p><img  srcset="data:image/svg+xml,%3Csvg%20xmlns='http://www.w3.org/2000/svg'%20viewBox='0%200%20300%20300'%3E%3C/svg%3E" data-src="http://img.smyhvae.com/20170801_2137.png" class="lozad post-image"src="http://img.smyhvae.com/20170801_2137.png"></p>
<p>上图中，我们发现：第二组中的第1个li，去贴靠第一组中的最后一个li了（我们本以为这些li会分成两排）。</p>
<p>这便引出我们要讲的：清除浮动的第一种方式。<br>那该怎么解决呢？</p>
<h3 id="方法1：给浮动元素的祖先元素加高度"><a href="#方法1：给浮动元素的祖先元素加高度" class="headerlink" title="方法1：给浮动元素的祖先元素加高度"></a>方法1：给浮动元素的祖先元素加高度</h3><p>造成前言中这个现象的根本原因是：li的<strong>父亲div没有设置高度</strong>，导致这两个div的高度均为0px（我们可以通过网页的审查元素进行查看）。div的高度为零，导致不能给自己浮动的孩子，撑起一个容器。</p>
<p>撑不起一个容器，导致自己的孩子没办法在自己的内部进行正确的浮动。</p>
<p>好，现在就算给这个div设置高度，可如果div自己的高度小于孩子的高度，也会出现不正常的现象：</p>
<p><img  srcset="data:image/svg+xml,%3Csvg%20xmlns='http://www.w3.org/2000/svg'%20viewBox='0%200%20300%20300'%3E%3C/svg%3E" data-src="http://img.smyhvae.com/20170801_2152.png" class="lozad post-image"src="http://img.smyhvae.com/20170801_2152.png"></p>
<p>给div设置一个正确的合适的高度（至少保证高度大于儿子的高度），就可以看到正确的现象：</p>
<p><img  srcset="data:image/svg+xml,%3Csvg%20xmlns='http://www.w3.org/2000/svg'%20viewBox='0%200%20300%20300'%3E%3C/svg%3E" data-src="http://img.smyhvae.com/20170801_2153.png" class="lozad post-image"src="http://img.smyhvae.com/20170801_2153.png"></p>
<p><strong>总结：</strong></p>
<p><strong>如果一个元素要浮动，那么它的祖先元素一定要有高度。</strong></p>
<p><strong>有高度的盒子，才能关住浮动</strong>。（记住这句过来人的经验之语）</p>
<p>只要浮动在一个有高度的盒子中，那么这个浮动就不会影响后面的浮动元素。所以就是清除浮动带来的影响了。</p>
<p><img  srcset="data:image/svg+xml,%3Csvg%20xmlns='http://www.w3.org/2000/svg'%20viewBox='0%200%20300%20300'%3E%3C/svg%3E" data-src="http://img.smyhvae.com/20170801_2200.png" class="lozad post-image"src="http://img.smyhvae.com/20170801_2200.png"></p>
<p><img  srcset="data:image/svg+xml,%3Csvg%20xmlns='http://www.w3.org/2000/svg'%20viewBox='0%200%20300%20300'%3E%3C/svg%3E" data-src="http://img.smyhvae.com/20170801_2201.png" class="lozad post-image"src="http://img.smyhvae.com/20170801_2201.png"></p>
<h3 id="方法2：clear-both"><a href="#方法2：clear-both" class="headerlink" title="方法2：clear:both;"></a>方法2：clear:both;</h3><p>网页制作中，高度height其实很少出现。为什么？因为能被内容撑高！也就是说，刚刚我们讲解的方法1，工作中用得很少。</p>
<p>那么，能不能不写height，也把浮动清除了呢？也让浮动之间，互不影响呢？</p>
<p>这个时候，我们可以使用<code>clear:both;</code>这个属性。如下：</p>
<p><img  srcset="data:image/svg+xml,%3Csvg%20xmlns='http://www.w3.org/2000/svg'%20viewBox='0%200%20300%20300'%3E%3C/svg%3E" data-src="http://img.smyhvae.com/20170801_2233.png" class="lozad post-image"src="http://img.smyhvae.com/20170801_2233.png"></p>
<pre class="highlight"><span class="line">clear:both;</span><br></pre>

<p>clear就是清除，both指的是左浮动、右浮动都要清除。<code>clear:both</code>的意思就是：<strong>不允许左侧和右侧有浮动对象。</strong></p>
<p>这种方法有一个非常大的、致命的问题，<strong>它所在的标签，margin属性失效了</strong>。读者可以试试看。</p>
<p>margin失效的本质原因是：上图中的box1和box2，高度为零。</p>
<h3 id="方法3：隔墙法"><a href="#方法3：隔墙法" class="headerlink" title="方法3：隔墙法"></a>方法3：隔墙法</h3><p>上面这个例子中，为了防止第二个div贴靠到第二个div，我们可以在这两个div中间用一个新的div隔开，然后给这个新的div设置<code>clear: both;</code>属性；同时，既然这个新的div无法设置margin属性，我们可以给它设置height，以达到margin的效果（曲线救国）。这便是隔墙法。</p>
<p>我们看看例子效果就知道了：</p>
<p><img  srcset="data:image/svg+xml,%3Csvg%20xmlns='http://www.w3.org/2000/svg'%20viewBox='0%200%20300%20300'%3E%3C/svg%3E" data-src="http://img.smyhvae.com/20170802_1109.png" class="lozad post-image"src="http://img.smyhvae.com/20170802_1109.png"></p>
<p>上图这个例子就是隔墙法。</p>
<p><strong>内墙法：</strong></p>
<p>近些年，有演化出了“内墙法”：</p>
<p><img  srcset="data:image/svg+xml,%3Csvg%20xmlns='http://www.w3.org/2000/svg'%20viewBox='0%200%20300%20300'%3E%3C/svg%3E" data-src="http://img.smyhvae.com/20170802_1123.png" class="lozad post-image"src="http://img.smyhvae.com/20170802_1123.png"></p>
<p>上面这个图非常重要，当作内墙法的公式，先记下来。</p>
<p>为了讲内墙法，我们先记住一句重要的话：<strong>一个父亲是不能被浮动的儿子撑出高度的</strong>。举例如下：</p>
<p>（1）我们在一个div里放一个有宽高的p，效果如下：（很简单）</p>
<p><img  srcset="data:image/svg+xml,%3Csvg%20xmlns='http://www.w3.org/2000/svg'%20viewBox='0%200%20300%20300'%3E%3C/svg%3E" data-src="http://img.smyhvae.com/20170802_1716.png" class="lozad post-image"src="http://img.smyhvae.com/20170802_1716.png"></p>
<p>（2）可如果在此基础之上，给p设置浮动，却发现父亲div没有高度了：</p>
<p><img  srcset="data:image/svg+xml,%3Csvg%20xmlns='http://www.w3.org/2000/svg'%20viewBox='0%200%20300%20300'%3E%3C/svg%3E" data-src="http://img.smyhvae.com/20170802_1730.png" class="lozad post-image"src="http://img.smyhvae.com/20170802_1730.png"></p>
<p>（3）此时，我么可以在div的里面放一个div（作为内墙），就可以让父亲div恢复高度：</p>
<p><img  srcset="data:image/svg+xml,%3Csvg%20xmlns='http://www.w3.org/2000/svg'%20viewBox='0%200%20300%20300'%3E%3C/svg%3E" data-src="http://img.smyhvae.com/20170802_1812.png" class="lozad post-image"src="http://img.smyhvae.com/20170802_1812.png"></p>
<p>于是，我们采用内墙法解决前言中的问题：</p>
<p><img  srcset="data:image/svg+xml,%3Csvg%20xmlns='http://www.w3.org/2000/svg'%20viewBox='0%200%20300%20300'%3E%3C/svg%3E" data-src="http://img.smyhvae.com/20170802_1834.png" class="lozad post-image"src="http://img.smyhvae.com/20170802_1834.png"></p>
<p>与外墙法相比，内墙法的优势（本质区别）在于：内墙法可以给它所在的家撑出宽度（让box1有高）。即：box1的高度可以自适应内容。</p>
<p>而外墙法，虽然一道墙可以把两个div隔开，但是这两个div没有高，也就是说，无法wrap_content。</p>
<h3 id="清除浮动方法4：overflow-hidden"><a href="#清除浮动方法4：overflow-hidden" class="headerlink" title="清除浮动方法4：overflow:hidden;"></a>清除浮动方法4：overflow:hidden;</h3><p>我们可以使用如下属性：</p>
<pre class="highlight"><span class="line">overflow:hidden;</span><br></pre>


<p>overflow即“溢出”， hidden即“隐藏”。这个属性的意思是“溢出隐藏”。顾名思义：所有溢出边框的内容，都要隐藏掉。如下：</p>
<p><img  srcset="data:image/svg+xml,%3Csvg%20xmlns='http://www.w3.org/2000/svg'%20viewBox='0%200%20300%20300'%3E%3C/svg%3E" data-src="http://img.smyhvae.com/20170804_1449.png" class="lozad post-image"src="http://img.smyhvae.com/20170804_1449.png"></p>
<p>上图显示，<code>overflow:hidden;</code>的本意是清除溢出到盒子外面的文字。但是，前端开发工程师发现了，它能做偏方。如下：</p>
<p>一个父亲不能被自己浮动的儿子，撑出高度。但是，只要给父亲加上<code>overflow:hidden</code>; 那么，父亲就能被儿子撑出高了。这是一个偏方。</p>
<p>举个例子：</p>
<p><img  srcset="data:image/svg+xml,%3Csvg%20xmlns='http://www.w3.org/2000/svg'%20viewBox='0%200%20300%20300'%3E%3C/svg%3E" data-src="http://img.smyhvae.com/20170804_1920.png" class="lozad post-image"src="http://img.smyhvae.com/20170804_1920.png"></p>
<p>那么对于前言中的例子，我们同样可以使用这一属性：</p>
<p><img  srcset="data:image/svg+xml,%3Csvg%20xmlns='http://www.w3.org/2000/svg'%20viewBox='0%200%20300%20300'%3E%3C/svg%3E" data-src="http://img.smyhvae.com/20170804_1934.png" class="lozad post-image"src="http://img.smyhvae.com/20170804_1934.png"></p>
<p>这一招，实际上生成了BFC。关于BFC的解释，详见本项目的另外一篇文章《前端面试&#x2F;CSS盒模型及BFC.md》。</p>
<h2 id="浮动清除的总结"><a href="#浮动清除的总结" class="headerlink" title="浮动清除的总结"></a>浮动清除的总结</h2><blockquote>
<p>我们在上一段讲了四种清除浮动的方法，本段来进行一个总结。</p>
</blockquote>
<p>浮动的元素，只能被有高度的盒子关住。 也就是说，如果盒子内部有浮动，这个盒子有高，那么妥妥的，浮动不会互相影响。</p>
<h3 id="1、加高法"><a href="#1、加高法" class="headerlink" title="1、加高法"></a>1、加高法</h3><p>工作上，我们绝对不会给所有的盒子加高度，这是因为麻烦，并且不能适应页面的快速变化。</p>
<pre class="highlight"><span class="line"><span class="tag">&lt;<span class="name">div</span>&gt;</span>     //设置height</span><br><span class="line">	<span class="tag">&lt;<span class="name">p</span>&gt;</span><span class="tag">&lt;/<span class="name">p</span>&gt;</span></span><br><span class="line">	<span class="tag">&lt;<span class="name">p</span>&gt;</span><span class="tag">&lt;/<span class="name">p</span>&gt;</span></span><br><span class="line">	<span class="tag">&lt;<span class="name">p</span>&gt;</span><span class="tag">&lt;/<span class="name">p</span>&gt;</span></span><br><span class="line"><span class="tag">&lt;/<span class="name">div</span>&gt;</span></span><br><span class="line"></span><br><span class="line"><span class="tag">&lt;<span class="name">div</span>&gt;</span>    //设置height</span><br><span class="line">	<span class="tag">&lt;<span class="name">p</span>&gt;</span><span class="tag">&lt;/<span class="name">p</span>&gt;</span></span><br><span class="line">	<span class="tag">&lt;<span class="name">p</span>&gt;</span><span class="tag">&lt;/<span class="name">p</span>&gt;</span></span><br><span class="line">	<span class="tag">&lt;<span class="name">p</span>&gt;</span><span class="tag">&lt;/<span class="name">p</span>&gt;</span></span><br><span class="line"><span class="tag">&lt;/<span class="name">div</span>&gt;</span></span><br></pre>


<h3 id="2、clear-both-法"><a href="#2、clear-both-法" class="headerlink" title="2、clear:both;法"></a>2、<code>clear:both;</code>法</h3><p>最简单的清除浮动的方法，就是给盒子增加clear:both；表示自己的内部元素，不受其他盒子的影响。</p>
<pre class="highlight"><span class="line"><span class="tag">&lt;<span class="name">div</span>&gt;</span></span><br><span class="line">	<span class="tag">&lt;<span class="name">p</span>&gt;</span><span class="tag">&lt;/<span class="name">p</span>&gt;</span></span><br><span class="line">	<span class="tag">&lt;<span class="name">p</span>&gt;</span><span class="tag">&lt;/<span class="name">p</span>&gt;</span></span><br><span class="line">	<span class="tag">&lt;<span class="name">p</span>&gt;</span><span class="tag">&lt;/<span class="name">p</span>&gt;</span></span><br><span class="line"><span class="tag">&lt;/<span class="name">div</span>&gt;</span></span><br><span class="line"></span><br><span class="line"><span class="tag">&lt;<span class="name">div</span>&gt;</span>   //clear:both;</span><br><span class="line">	<span class="tag">&lt;<span class="name">p</span>&gt;</span><span class="tag">&lt;/<span class="name">p</span>&gt;</span></span><br><span class="line">	<span class="tag">&lt;<span class="name">p</span>&gt;</span><span class="tag">&lt;/<span class="name">p</span>&gt;</span></span><br><span class="line">	<span class="tag">&lt;<span class="name">p</span>&gt;</span><span class="tag">&lt;/<span class="name">p</span>&gt;</span></span><br><span class="line"><span class="tag">&lt;/<span class="name">div</span>&gt;</span></span><br></pre>

<p>浮动确实被清除了，不会互相影响了。但是有一个问题，就是margin失效。两个div之间，没有任何的间隙了。</p>
<h3 id="3、隔墙法"><a href="#3、隔墙法" class="headerlink" title="3、隔墙法"></a>3、隔墙法</h3><p>在两部分浮动元素中间，建一个墙。隔开两部分浮动，让后面的浮动元素，不去追前面的浮动元素。<br>墙用自己的身体当做了间隙。</p>
<pre class="highlight"><span class="line"><span class="tag">&lt;<span class="name">div</span>&gt;</span></span><br><span class="line">	<span class="tag">&lt;<span class="name">p</span>&gt;</span><span class="tag">&lt;/<span class="name">p</span>&gt;</span></span><br><span class="line">	<span class="tag">&lt;<span class="name">p</span>&gt;</span><span class="tag">&lt;/<span class="name">p</span>&gt;</span></span><br><span class="line">	<span class="tag">&lt;<span class="name">p</span>&gt;</span><span class="tag">&lt;/<span class="name">p</span>&gt;</span></span><br><span class="line"><span class="tag">&lt;/<span class="name">div</span>&gt;</span></span><br><span class="line"></span><br><span class="line"><span class="tag">&lt;<span class="name">div</span> <span class="attr">class</span>=<span class="string">&quot;cl h10&quot;</span>&gt;</span><span class="tag">&lt;/<span class="name">div</span>&gt;</span></span><br><span class="line"></span><br><span class="line"><span class="tag">&lt;<span class="name">div</span>&gt;</span></span><br><span class="line">	<span class="tag">&lt;<span class="name">p</span>&gt;</span><span class="tag">&lt;/<span class="name">p</span>&gt;</span></span><br><span class="line">	<span class="tag">&lt;<span class="name">p</span>&gt;</span><span class="tag">&lt;/<span class="name">p</span>&gt;</span></span><br><span class="line">	<span class="tag">&lt;<span class="name">p</span>&gt;</span><span class="tag">&lt;/<span class="name">p</span>&gt;</span></span><br><span class="line"><span class="tag">&lt;/<span class="name">div</span>&gt;</span></span><br></pre>

<p>我们发现，隔墙法好用，但是第一个div，还是没有高度。如果我们现在想让第一个div，自动根据自己的儿子撑出高度，我们就要想一些“小伎俩”。</p>
<p>内墙法：</p>
<pre class="highlight"><span class="line"><span class="tag">&lt;<span class="name">div</span>&gt;</span></span><br><span class="line">	<span class="tag">&lt;<span class="name">p</span>&gt;</span><span class="tag">&lt;/<span class="name">p</span>&gt;</span></span><br><span class="line">	<span class="tag">&lt;<span class="name">p</span>&gt;</span><span class="tag">&lt;/<span class="name">p</span>&gt;</span></span><br><span class="line">	<span class="tag">&lt;<span class="name">p</span>&gt;</span><span class="tag">&lt;/<span class="name">p</span>&gt;</span></span><br><span class="line">	<span class="tag">&lt;<span class="name">div</span> <span class="attr">class</span>=<span class="string">&quot;cl h10&quot;</span>&gt;</span><span class="tag">&lt;/<span class="name">div</span>&gt;</span></span><br><span class="line"><span class="tag">&lt;/<span class="name">div</span>&gt;</span></span><br><span class="line"></span><br><span class="line"><span class="tag">&lt;<span class="name">div</span>&gt;</span></span><br><span class="line">	<span class="tag">&lt;<span class="name">p</span>&gt;</span><span class="tag">&lt;/<span class="name">p</span>&gt;</span></span><br><span class="line">	<span class="tag">&lt;<span class="name">p</span>&gt;</span><span class="tag">&lt;/<span class="name">p</span>&gt;</span></span><br><span class="line">	<span class="tag">&lt;<span class="name">p</span>&gt;</span><span class="tag">&lt;/<span class="name">p</span>&gt;</span></span><br><span class="line"><span class="tag">&lt;/<span class="name">div</span>&gt;</span></span><br></pre>

<p>内墙法的优点就是，不仅仅能够让后部分的p不去追前部分的p了，并且能把第一个div撑出高度。这样，这个div的背景、边框就能够根据p的高度来撑开了。</p>
<h3 id="4、overflow-hidden"><a href="#4、overflow-hidden" class="headerlink" title="4、overflow:hidden;"></a>4、<code>overflow:hidden;</code></h3><p>这个属性的本意，就是将所有溢出盒子的内容，隐藏掉。但是，我们发现这个东西能够用于浮动的清除。<br>我们知道，一个父亲，不能被自己浮动的儿子撑出高度，但是，如果这个父亲加上了overflow:hidden；那么这个父亲就能够被浮动的儿子撑出高度了。这个现象，不能解释，就是浏览器的偏方。<br>并且,overflow:hidden;能够让margin生效。</p>
<p><strong>清除浮动的例子：</strong></p>
<p>我们现在举个例子，要求实现下图中无序列表部分的效果：</p>
<p><img  srcset="data:image/svg+xml,%3Csvg%20xmlns='http://www.w3.org/2000/svg'%20viewBox='0%200%20300%20300'%3E%3C/svg%3E" data-src="http://img.smyhvae.com/20170804_2321.png" class="lozad post-image"src="http://img.smyhvae.com/20170804_2321.png"></p>
<p>对比一下我们讲的四种清除浮动的方法。如果用外墙法，ul中不能插入div标签，因为ul中只能插入li，如果插入li的墙，会浪费语义。如果用内墙法，不美观。综合对比，还是用第四种方法来实现吧，这会让标签显得极其干净整洁：</p>
<p><img  srcset="data:image/svg+xml,%3Csvg%20xmlns='http://www.w3.org/2000/svg'%20viewBox='0%200%20300%20300'%3E%3C/svg%3E" data-src="http://img.smyhvae.com/20170805_1615.png" class="lozad post-image"src="http://img.smyhvae.com/20170805_1615.png"></p>
<p>上方代码中，如果没有加<code>overflow:hidden;</code>，那么第二行的li会紧跟着第一行li的后面。</p>
<h2 id="浏览器的兼容性问题"><a href="#浏览器的兼容性问题" class="headerlink" title="浏览器的兼容性问题"></a>浏览器的兼容性问题</h2><blockquote>
<p>讲一下上述知识点涉及到的浏览器兼容问题。</p>
</blockquote>
<h3 id="兼容性1（微型盒子）"><a href="#兼容性1（微型盒子）" class="headerlink" title="兼容性1（微型盒子）"></a>兼容性1（微型盒子）</h3><p><strong>兼容性的第一条</strong>：IE6不支持小于12px的盒子，任何小于12px的盒子，在IE6中看都大。即：IE 6不支持微型盒子。</p>
<p>举个例子。我们设置一个height为 5px 、宽度为 200px的盒子，看下在IE 8和 IE 6中的显示效果：</p>
<p><img  srcset="data:image/svg+xml,%3Csvg%20xmlns='http://www.w3.org/2000/svg'%20viewBox='0%200%20300%20300'%3E%3C/svg%3E" data-src="http://img.smyhvae.com/20170805_1726.png" class="lozad post-image"src="http://img.smyhvae.com/20170805_1726.png"></p>
<p>解决办法很简单，就是将盒子的字号大小，设置为<strong>小于盒子的高</strong>，比如，如果盒子的高为5px，那就把font-size设置为0px(0px &lt; 5px)。如下：</p>
<pre class="highlight"><span class="line">height: 5px;</span><br><span class="line">_font-size: 0px;</span><br></pre>

<p>我们现在介绍一下浏览器hack。hack就是“黑客”，就是使用浏览器提供的后门，针对某一种浏览器做兼容。</p>
<p>IE6留了一个<strong>后门</strong>：只要给css属性之前，加上<strong>下划线</strong>，这个属性就是IE6的专有属性。</p>
<p>比如说，我们给背景颜色这个属性加上下划线，就变成了<code>_background-color: green;</code>。效果如下：</p>
<p><img  srcset="data:image/svg+xml,%3Csvg%20xmlns='http://www.w3.org/2000/svg'%20viewBox='0%200%20300%20300'%3E%3C/svg%3E" data-src="http://img.smyhvae.com/20170805_2026.png" class="lozad post-image"src="http://img.smyhvae.com/20170805_2026.png"></p>
<p>于是乎，为了解决微型盒子（即height小于12px）的问题，正确写法：（注意不要忘记下划线）</p>
<pre class="highlight"><span class="line">height: 10px;</span><br><span class="line">_font-size:0;</span><br></pre>



<h3 id="兼容性2"><a href="#兼容性2" class="headerlink" title="兼容性2"></a>兼容性2</h3><p><strong>兼容性的第二条：</strong>IE6不支持用<code>overflow:hidden;</code>来清除浮动。</p>
<p>解决办法，以毒攻毒。追加一条：</p>
<pre class="highlight"><span class="line">_zoom:1;</span><br></pre>

<p>完整写法：</p>
<pre class="highlight"><span class="line">overflow: hidden;</span><br><span class="line">_zoom:1;</span><br></pre>

<p>实际上，<code>_zoom:1;</code>能够触发浏览器hasLayout机制。这个机制，不要深究了，因为只有IE6有。我们只需要让IE6好用，具体的实现机制，可以自行查阅。</p>
<p>需要强调的是，<code>overflow:hidden;</code>的本意，就是让溢出盒子的border的内容隐藏，这个功能是IE6兼容的。不兼容的是<code>overflow:hidden;</code>清除浮动的时候。</p>
<p><strong>总结：</strong></p>
<p>我们刚才学习的两个IE6的兼容问题，都是通过多写一条hack来解决的，这个我们称为伴生属性，即两个属性，要写一起写。</p>
<p>属性1：</p>
<pre class="highlight"><span class="line">height:6px;</span><br><span class="line">_font-size:0;</span><br></pre>

<p>属性2：</p>
<pre class="highlight"><span class="line">overflow:hidden;</span><br><span class="line">_zoom:1;</span><br></pre>


<h2 id="margin相关"><a href="#margin相关" class="headerlink" title="margin相关"></a>margin相关</h2><blockquote>
<p>我们来讲一下浮动中和margin相关的知识。</p>
</blockquote>
<h3 id="margin塌陷-x2F-margin重叠"><a href="#margin塌陷-x2F-margin重叠" class="headerlink" title="margin塌陷&#x2F;margin重叠"></a>margin塌陷&#x2F;margin重叠</h3><p><strong>标准文档流中，竖直方向的margin不叠加，取</strong>较大的值**作为margin(水平方向的margin是可以叠加的，即水平方向没有塌陷现象)。如下图所示：</p>
<p><img  srcset="data:image/svg+xml,%3Csvg%20xmlns='http://www.w3.org/2000/svg'%20viewBox='0%200%20300%20300'%3E%3C/svg%3E" data-src="http://img.smyhvae.com/20170805_0904.png" class="lozad post-image"src="http://img.smyhvae.com/20170805_0904.png"></p>
<p>如果不在标准流，比如盒子都浮动了，那么两个盒子之间是没有塌陷现象的。</p>
<h3 id="盒子居中margin-0-auto"><a href="#盒子居中margin-0-auto" class="headerlink" title="盒子居中margin:0 auto;"></a>盒子居中<code>margin:0 auto;</code></h3><p>margin的值可以为auto，表示自动。当left、right两个方向都是auto的时候，盒子居中了：</p>
<pre class="highlight"><span class="line">margin-left: auto;</span><br><span class="line">margin-right: auto;</span><br></pre>

<p>盒子居中的简写为：</p>
<pre class="highlight"><span class="line">margin:0 auto;</span><br></pre>

<p>对上方代码的理解：上下的margin为0，左右的margin都尽可能的大，于是就居中了。</p>
<p>注意：</p>
<ul>
<li>（1）只有标准流的盒子，才能使用<code>margin:0 auto;</code>居中。也就是说，当一个盒子浮动了、绝对定位了、固定定位了，都不能使用margin:0 auto;</li>
<li>（2）使用<code>margin:0 auto;</code>的盒子，必须有width，有明确的width。（可以这样理解，如果没有明确的width，那么它的width就是霸占整行，没有意义）</li>
<li>（3）<code>margin:0 auto;</code>是让盒子居中，不是让盒子里的文本居中。文本的居中，要使用<code>text-align:center;</code></li>
</ul>
<p>对上面的第三条总结一下：（非常重要）</p>
<pre class="highlight"><span class="line">margin:0 auto;    //让这个div自己在大容器中的水平方向上居中。</span><br><span class="line">text-align: center;  //让这个div内部的文本居中。</span><br></pre>

<p>顺便普及一下知识，text-align还有：</p>
<pre class="highlight"><span class="line">text-align:left;     //没啥用，因为默认居左</span><br><span class="line">text-align:right;    //文本居右</span><br></pre>



<h3 id="善于使用父亲的padding，而不是儿子的margin"><a href="#善于使用父亲的padding，而不是儿子的margin" class="headerlink" title="善于使用父亲的padding，而不是儿子的margin"></a>善于使用父亲的padding，而不是儿子的margin</h3><p>我们来看一个奇怪的现象。现在有下面这样一个结构：（div中放一个p）</p>
<pre class="highlight"><span class="line">&lt;div&gt;</span><br><span class="line">	&lt;p&gt;&lt;/p&gt;</span><br><span class="line">&lt;/div&gt;</span><br></pre>

<p>上面的结构中，我们尝试通过给儿子<code>p</code>一个<code>margin-top:50px;</code>的属性，让其与父亲保持50px的上边距。结果却看到了下面的奇怪的现象：</p>
<p><img  srcset="data:image/svg+xml,%3Csvg%20xmlns='http://www.w3.org/2000/svg'%20viewBox='0%200%20300%20300'%3E%3C/svg%3E" data-src="http://img.smyhvae.com/20170806_1537.png" class="lozad post-image"src="http://img.smyhvae.com/20170806_1537.png"></p>
<p>此时我们给父亲div加一个border属性，就正常了：</p>
<p><img  srcset="data:image/svg+xml,%3Csvg%20xmlns='http://www.w3.org/2000/svg'%20viewBox='0%200%20300%20300'%3E%3C/svg%3E" data-src="http://img.smyhvae.com/20170806_1544.png" class="lozad post-image"src="http://img.smyhvae.com/20170806_1544.png"></p>
<p>如果父亲没有border，那么儿子的margin实际上踹的是“流”，踹的是这“行”。所以，父亲整体也掉下来了。</p>
<p><strong>margin这个属性，本质上描述的是兄弟和兄弟之间的距离； 最好不要用这个marign表达父子之间的距离。</strong></p>
<p>所以，如果要表达父子之间的距离，我们一定要善于使用父亲的padding，而不是儿子的margin。</p>
<h2 id="关于margin的IE6兼容问题"><a href="#关于margin的IE6兼容问题" class="headerlink" title="关于margin的IE6兼容问题"></a>关于margin的IE6兼容问题</h2><h3 id="IE6的双倍margin的bug："><a href="#IE6的双倍margin的bug：" class="headerlink" title="IE6的双倍margin的bug："></a>IE6的双倍margin的bug：</h3><p>当出现连续浮动的元素，携带与浮动方向相同的margin时，队首的元素，会双倍marign。</p>
<pre class="highlight"><span class="line">&lt;ul&gt;</span><br><span class="line">	&lt;li&gt;&lt;/li&gt;</span><br><span class="line">	&lt;li&gt;&lt;/li&gt;</span><br><span class="line">	&lt;li&gt;&lt;/li&gt;</span><br><span class="line">&lt;/ul&gt;</span><br></pre>

<p><img  srcset="data:image/svg+xml,%3Csvg%20xmlns='http://www.w3.org/2000/svg'%20viewBox='0%200%20300%20300'%3E%3C/svg%3E" data-src="http://img.smyhvae.com/20170806_1558.png" class="lozad post-image"src="http://img.smyhvae.com/20170806_1558.png"></p>
<p>解决方案：</p>
<p>（1）使浮动的方向和margin的方向，相反。</p>
<p>所以，你就会发现，我们特别喜欢，浮动的方向和margin的方向相反。并且，前端开发工程师，把这个当做习惯了。</p>
<pre class="highlight"><span class="line">float: left;</span><br><span class="line">margin-right: 40px;</span><br></pre>



<p>（2）使用hack：（没必要，别惯着这个IE6）</p>
<p>单独给队首的元素，写一个一半的margin：</p>
<pre class="highlight"><span class="line">&lt;li class=&quot;no1&quot;&gt;&lt;/li&gt;</span><br></pre>

<pre class="highlight"><span class="line">ul li.no1&#123;</span><br><span class="line">	_margin-left:20px;</span><br><span class="line">&#125;</span><br></pre>

<p>PS：双倍margin的问题，面试经常问哦。</p>
<h3 id="IE6的3px-bug"><a href="#IE6的3px-bug" class="headerlink" title="IE6的3px bug"></a>IE6的3px bug</h3><p><img  srcset="data:image/svg+xml,%3Csvg%20xmlns='http://www.w3.org/2000/svg'%20viewBox='0%200%20300%20300'%3E%3C/svg%3E" data-src="http://img.smyhvae.com/20170806_1616.png" class="lozad post-image"src="http://img.smyhvae.com/20170806_1616.png"></p>
<p>解决办法：不用管，因为根本就不允许用儿子踹父亲（即描述父子之间的距离，请用padding，而不是margin）。所以，如果你出现了3px bug，说明你的代码不标准。</p>
<p>IE6，千万不要跟他死坑、较劲，它不配。 格调要高，我们讲IE6的兼容性问题，就是为了增加面试的成功率，不是为了成为IE6的专家。</p>
<h2 id="Fireworks和others"><a href="#Fireworks和others" class="headerlink" title="Fireworks和others"></a>Fireworks和others</h2><h3 id="Fireworks"><a href="#Fireworks" class="headerlink" title="Fireworks"></a>Fireworks</h3><p>fireworks是Adobe公司的一个设计软件。功能非常多，我们以后用啥讲啥。Fireworks的默认文件格式是png。</p>
<p>标尺的快捷键：Ctrl + Alt+ R</p>
<h3 id="others"><a href="#others" class="headerlink" title="others"></a>others</h3><p>首行缩进两个汉字：</p>
<pre class="highlight"><span class="line">text-indent: 2em;</span><br></pre>

<p>上方属性中，单位比较奇怪，叫做em，em就是汉字的一个宽度。indent的意思是缩进。</p>
<h2 id="我的公众号"><a href="#我的公众号" class="headerlink" title="我的公众号"></a>我的公众号</h2><p>想学习<strong>更多技能</strong>？不妨关注我的微信公众号：<strong>千古壹号</strong>（id：<code>qianguyihao</code>）。</p>
<p>扫一扫，你将发现另一个全新的世界，而这将是一场美丽的意外：</p>
<p><img  srcset="data:image/svg+xml,%3Csvg%20xmlns='http://www.w3.org/2000/svg'%20viewBox='0%200%20300%20300'%3E%3C/svg%3E" data-src="http://img.smyhvae.com/20190101.png" class="lozad post-image"src="http://img.smyhvae.com/20190101.png"></p>

  </div>
  <div>
    
      <div 
        class="post-note note-warning copyright" 
        style="margin-top: 42px">
        <p>
          <span style="font-weight: bold;">作者：</span><a 
            target="_blank" 
            rel="nofollow noopener noreferrer" 
            href="/about">
            zwl5670
          </a>
        </p>
        <p>
          <span style="font-weight: bold;">文章链接：</span><a 
            target="_blank" 
            rel="nofollow noopener noreferrer" 
            href="https://zwl5670.gitee.io/2002/01/01/02-CSS%E5%9F%BA%E7%A1%80/07-%E6%B5%AE%E5%8A%A8/">
            https://zwl5670.gitee.io/2002/01/01/02-CSS%E5%9F%BA%E7%A1%80/07-%E6%B5%AE%E5%8A%A8/
          </a>
        </p>
        <p><span style="font-weight: bold;">版权声明：</span>本博客所有文章除特别声明外，均采用<a target="_blank" rel="noopener" href="https://creativecommons.org/licenses/by-nc-sa/4.0/deed.zh">CC BY-NC-SA 4.0 协议</a>。转载请注明出处！</p>
      </div>
    
  </div>
</article>
<div class="nav">
  
    <div class="nav-item-prev">
      <a 
        href="/2002/01/01/02-CSS%E5%9F%BA%E7%A1%80/06-CSS%E7%9B%92%E6%A8%A1%E5%9E%8B%E8%AF%A6%E8%A7%A3/" 
        class="nav-link">
        <i class="iconfont icon-left nav-prev-icon"></i>
        <div>
          <div class="nav-label">上一篇</div>
          
            <div class="nav-title">06-CSS盒模型详解 </div>
          
        </div>
      </a>
    </div>
  
  
    <div class="nav-item-next">
      <a 
        href="/2002/01/01/02-CSS%E5%9F%BA%E7%A1%80/08-CSS%E5%B1%9E%E6%80%A7%EF%BC%9A%E5%AE%9A%E4%BD%8D%E5%B1%9E%E6%80%A7/" 
        class="nav-link">
        <div>
          <div class="nav-label">下一篇</div>
          
            <div class="nav-title">08-CSS属性：定位属性 </div>
          
        </div>
        <i class="iconfont icon-right nav-next-icon"></i>
      </a>
    </div>
  
</div>

<div 
  class="card card-content toc-card" 
  id="mobiletoc">
  <div class="toc-header">
  <i 
    class="iconfont icon-menu" 
    style="padding-right: 2px;">
  </i>目录
</div>
<ol class="toc"><li class="toc-item toc-level-2"><a class="toc-link" href="#%E6%96%87%E6%9C%AC%E4%B8%BB%E8%A6%81%E5%86%85%E5%AE%B9"><span class="toc-text">文本主要内容</span></a></li><li class="toc-item toc-level-2"><a class="toc-link" href="#%E6%A0%87%E5%87%86%E6%96%87%E6%A1%A3%E6%B5%81"><span class="toc-text">标准文档流</span></a><ol class="toc-child"><li class="toc-item toc-level-3"><a class="toc-link" href="#%E6%A0%87%E5%87%86%E6%96%87%E6%A1%A3%E6%B5%81%E7%9A%84%E7%89%B9%E6%80%A7"><span class="toc-text">标准文档流的特性</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#%E8%A1%8C%E5%86%85%E5%85%83%E7%B4%A0%E5%92%8C%E5%9D%97%E7%BA%A7%E5%85%83%E7%B4%A0"><span class="toc-text">行内元素和块级元素</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#%E8%A1%8C%E5%86%85%E5%85%83%E7%B4%A0%E5%92%8C%E5%9D%97%E7%BA%A7%E5%85%83%E7%B4%A0%E7%9A%84%E7%9B%B8%E4%BA%92%E8%BD%AC%E6%8D%A2"><span class="toc-text">行内元素和块级元素的相互转换</span></a><ol class="toc-child"><li class="toc-item toc-level-4"><a class="toc-link" href="#%E5%9D%97%E7%BA%A7%E5%85%83%E7%B4%A0%E5%8F%AF%E4%BB%A5%E8%BD%AC%E6%8D%A2%E4%B8%BA%E8%A1%8C%E5%86%85%E5%85%83%E7%B4%A0%EF%BC%9A"><span class="toc-text">块级元素可以转换为行内元素：</span></a></li><li class="toc-item toc-level-4"><a class="toc-link" href="#%E8%A1%8C%E5%86%85%E5%85%83%E7%B4%A0%E8%BD%AC%E6%8D%A2%E4%B8%BA%E5%9D%97%E7%BA%A7%E5%85%83%E7%B4%A0%EF%BC%9A"><span class="toc-text">行内元素转换为块级元素：</span></a></li></ol></li></ol></li><li class="toc-item toc-level-2"><a class="toc-link" href="#%E6%B5%AE%E5%8A%A8%E7%9A%84%E6%80%A7%E8%B4%A8"><span class="toc-text">浮动的性质</span></a><ol class="toc-child"><li class="toc-item toc-level-3"><a class="toc-link" href="#%E6%80%A7%E8%B4%A81%EF%BC%9A%E6%B5%AE%E5%8A%A8%E7%9A%84%E5%85%83%E7%B4%A0%E8%84%B1%E6%A0%87"><span class="toc-text">性质1：浮动的元素脱标</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#%E6%80%A7%E8%B4%A82%EF%BC%9A%E6%B5%AE%E5%8A%A8%E7%9A%84%E5%85%83%E7%B4%A0%E4%BA%92%E7%9B%B8%E8%B4%B4%E9%9D%A0"><span class="toc-text">性质2：浮动的元素互相贴靠</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#%E6%80%A7%E8%B4%A83%EF%BC%9A%E6%B5%AE%E5%8A%A8%E7%9A%84%E5%85%83%E7%B4%A0%E6%9C%89%E2%80%9C%E5%AD%97%E5%9B%B4%E2%80%9D%E6%95%88%E6%9E%9C"><span class="toc-text">性质3：浮动的元素有“字围”效果</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#%E6%80%A7%E8%B4%A84%EF%BC%9A%E6%94%B6%E7%BC%A9"><span class="toc-text">性质4：收缩</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#%E6%B5%AE%E5%8A%A8%E7%9A%84%E8%A1%A5%E5%85%85%EF%BC%88%E5%81%9A%E7%BD%91%E7%AB%99%E6%97%B6%E6%B3%A8%E6%84%8F%EF%BC%89"><span class="toc-text">浮动的补充（做网站时注意）</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#%E5%B8%83%E7%BD%AE%E4%B8%80%E4%B8%AA%E4%BD%9C%E4%B8%9A"><span class="toc-text">布置一个作业</span></a></li></ol></li><li class="toc-item toc-level-2"><a class="toc-link" href="#%E6%B5%AE%E5%8A%A8%E7%9A%84%E6%B8%85%E9%99%A4"><span class="toc-text">浮动的清除</span></a><ol class="toc-child"><li class="toc-item toc-level-3"><a class="toc-link" href="#%E5%89%8D%E8%A8%80"><span class="toc-text">前言</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#%E6%96%B9%E6%B3%951%EF%BC%9A%E7%BB%99%E6%B5%AE%E5%8A%A8%E5%85%83%E7%B4%A0%E7%9A%84%E7%A5%96%E5%85%88%E5%85%83%E7%B4%A0%E5%8A%A0%E9%AB%98%E5%BA%A6"><span class="toc-text">方法1：给浮动元素的祖先元素加高度</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#%E6%96%B9%E6%B3%952%EF%BC%9Aclear-both"><span class="toc-text">方法2：clear:both;</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#%E6%96%B9%E6%B3%953%EF%BC%9A%E9%9A%94%E5%A2%99%E6%B3%95"><span class="toc-text">方法3：隔墙法</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#%E6%B8%85%E9%99%A4%E6%B5%AE%E5%8A%A8%E6%96%B9%E6%B3%954%EF%BC%9Aoverflow-hidden"><span class="toc-text">清除浮动方法4：overflow:hidden;</span></a></li></ol></li><li class="toc-item toc-level-2"><a class="toc-link" href="#%E6%B5%AE%E5%8A%A8%E6%B8%85%E9%99%A4%E7%9A%84%E6%80%BB%E7%BB%93"><span class="toc-text">浮动清除的总结</span></a><ol class="toc-child"><li class="toc-item toc-level-3"><a class="toc-link" href="#1%E3%80%81%E5%8A%A0%E9%AB%98%E6%B3%95"><span class="toc-text">1、加高法</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#2%E3%80%81clear-both-%E6%B3%95"><span class="toc-text">2、clear:both;法</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#3%E3%80%81%E9%9A%94%E5%A2%99%E6%B3%95"><span class="toc-text">3、隔墙法</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#4%E3%80%81overflow-hidden"><span class="toc-text">4、overflow:hidden;</span></a></li></ol></li><li class="toc-item toc-level-2"><a class="toc-link" href="#%E6%B5%8F%E8%A7%88%E5%99%A8%E7%9A%84%E5%85%BC%E5%AE%B9%E6%80%A7%E9%97%AE%E9%A2%98"><span class="toc-text">浏览器的兼容性问题</span></a><ol class="toc-child"><li class="toc-item toc-level-3"><a class="toc-link" href="#%E5%85%BC%E5%AE%B9%E6%80%A71%EF%BC%88%E5%BE%AE%E5%9E%8B%E7%9B%92%E5%AD%90%EF%BC%89"><span class="toc-text">兼容性1（微型盒子）</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#%E5%85%BC%E5%AE%B9%E6%80%A72"><span class="toc-text">兼容性2</span></a></li></ol></li><li class="toc-item toc-level-2"><a class="toc-link" href="#margin%E7%9B%B8%E5%85%B3"><span class="toc-text">margin相关</span></a><ol class="toc-child"><li class="toc-item toc-level-3"><a class="toc-link" href="#margin%E5%A1%8C%E9%99%B7-x2F-margin%E9%87%8D%E5%8F%A0"><span class="toc-text">margin塌陷&#x2F;margin重叠</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#%E7%9B%92%E5%AD%90%E5%B1%85%E4%B8%ADmargin-0-auto"><span class="toc-text">盒子居中margin:0 auto;</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#%E5%96%84%E4%BA%8E%E4%BD%BF%E7%94%A8%E7%88%B6%E4%BA%B2%E7%9A%84padding%EF%BC%8C%E8%80%8C%E4%B8%8D%E6%98%AF%E5%84%BF%E5%AD%90%E7%9A%84margin"><span class="toc-text">善于使用父亲的padding，而不是儿子的margin</span></a></li></ol></li><li class="toc-item toc-level-2"><a class="toc-link" href="#%E5%85%B3%E4%BA%8Emargin%E7%9A%84IE6%E5%85%BC%E5%AE%B9%E9%97%AE%E9%A2%98"><span class="toc-text">关于margin的IE6兼容问题</span></a><ol class="toc-child"><li class="toc-item toc-level-3"><a class="toc-link" href="#IE6%E7%9A%84%E5%8F%8C%E5%80%8Dmargin%E7%9A%84bug%EF%BC%9A"><span class="toc-text">IE6的双倍margin的bug：</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#IE6%E7%9A%843px-bug"><span class="toc-text">IE6的3px bug</span></a></li></ol></li><li class="toc-item toc-level-2"><a class="toc-link" href="#Fireworks%E5%92%8Cothers"><span class="toc-text">Fireworks和others</span></a><ol class="toc-child"><li class="toc-item toc-level-3"><a class="toc-link" href="#Fireworks"><span class="toc-text">Fireworks</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#others"><span class="toc-text">others</span></a></li></ol></li><li class="toc-item toc-level-2"><a class="toc-link" href="#%E6%88%91%E7%9A%84%E5%85%AC%E4%BC%97%E5%8F%B7"><span class="toc-text">我的公众号</span></a></li></ol>
</div>
            </main>
            <aside class="right-column">
              <div class="sticky-widescreen">
  
  
    <article class="card card-content toc-card">
      <div class="toc-header">
  <i 
    class="iconfont icon-menu" 
    style="padding-right: 2px;">
  </i>目录
</div>
<ol class="toc"><li class="toc-item toc-level-2"><a class="toc-link" href="#%E6%96%87%E6%9C%AC%E4%B8%BB%E8%A6%81%E5%86%85%E5%AE%B9"><span class="toc-text">文本主要内容</span></a></li><li class="toc-item toc-level-2"><a class="toc-link" href="#%E6%A0%87%E5%87%86%E6%96%87%E6%A1%A3%E6%B5%81"><span class="toc-text">标准文档流</span></a><ol class="toc-child"><li class="toc-item toc-level-3"><a class="toc-link" href="#%E6%A0%87%E5%87%86%E6%96%87%E6%A1%A3%E6%B5%81%E7%9A%84%E7%89%B9%E6%80%A7"><span class="toc-text">标准文档流的特性</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#%E8%A1%8C%E5%86%85%E5%85%83%E7%B4%A0%E5%92%8C%E5%9D%97%E7%BA%A7%E5%85%83%E7%B4%A0"><span class="toc-text">行内元素和块级元素</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#%E8%A1%8C%E5%86%85%E5%85%83%E7%B4%A0%E5%92%8C%E5%9D%97%E7%BA%A7%E5%85%83%E7%B4%A0%E7%9A%84%E7%9B%B8%E4%BA%92%E8%BD%AC%E6%8D%A2"><span class="toc-text">行内元素和块级元素的相互转换</span></a><ol class="toc-child"><li class="toc-item toc-level-4"><a class="toc-link" href="#%E5%9D%97%E7%BA%A7%E5%85%83%E7%B4%A0%E5%8F%AF%E4%BB%A5%E8%BD%AC%E6%8D%A2%E4%B8%BA%E8%A1%8C%E5%86%85%E5%85%83%E7%B4%A0%EF%BC%9A"><span class="toc-text">块级元素可以转换为行内元素：</span></a></li><li class="toc-item toc-level-4"><a class="toc-link" href="#%E8%A1%8C%E5%86%85%E5%85%83%E7%B4%A0%E8%BD%AC%E6%8D%A2%E4%B8%BA%E5%9D%97%E7%BA%A7%E5%85%83%E7%B4%A0%EF%BC%9A"><span class="toc-text">行内元素转换为块级元素：</span></a></li></ol></li></ol></li><li class="toc-item toc-level-2"><a class="toc-link" href="#%E6%B5%AE%E5%8A%A8%E7%9A%84%E6%80%A7%E8%B4%A8"><span class="toc-text">浮动的性质</span></a><ol class="toc-child"><li class="toc-item toc-level-3"><a class="toc-link" href="#%E6%80%A7%E8%B4%A81%EF%BC%9A%E6%B5%AE%E5%8A%A8%E7%9A%84%E5%85%83%E7%B4%A0%E8%84%B1%E6%A0%87"><span class="toc-text">性质1：浮动的元素脱标</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#%E6%80%A7%E8%B4%A82%EF%BC%9A%E6%B5%AE%E5%8A%A8%E7%9A%84%E5%85%83%E7%B4%A0%E4%BA%92%E7%9B%B8%E8%B4%B4%E9%9D%A0"><span class="toc-text">性质2：浮动的元素互相贴靠</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#%E6%80%A7%E8%B4%A83%EF%BC%9A%E6%B5%AE%E5%8A%A8%E7%9A%84%E5%85%83%E7%B4%A0%E6%9C%89%E2%80%9C%E5%AD%97%E5%9B%B4%E2%80%9D%E6%95%88%E6%9E%9C"><span class="toc-text">性质3：浮动的元素有“字围”效果</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#%E6%80%A7%E8%B4%A84%EF%BC%9A%E6%94%B6%E7%BC%A9"><span class="toc-text">性质4：收缩</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#%E6%B5%AE%E5%8A%A8%E7%9A%84%E8%A1%A5%E5%85%85%EF%BC%88%E5%81%9A%E7%BD%91%E7%AB%99%E6%97%B6%E6%B3%A8%E6%84%8F%EF%BC%89"><span class="toc-text">浮动的补充（做网站时注意）</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#%E5%B8%83%E7%BD%AE%E4%B8%80%E4%B8%AA%E4%BD%9C%E4%B8%9A"><span class="toc-text">布置一个作业</span></a></li></ol></li><li class="toc-item toc-level-2"><a class="toc-link" href="#%E6%B5%AE%E5%8A%A8%E7%9A%84%E6%B8%85%E9%99%A4"><span class="toc-text">浮动的清除</span></a><ol class="toc-child"><li class="toc-item toc-level-3"><a class="toc-link" href="#%E5%89%8D%E8%A8%80"><span class="toc-text">前言</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#%E6%96%B9%E6%B3%951%EF%BC%9A%E7%BB%99%E6%B5%AE%E5%8A%A8%E5%85%83%E7%B4%A0%E7%9A%84%E7%A5%96%E5%85%88%E5%85%83%E7%B4%A0%E5%8A%A0%E9%AB%98%E5%BA%A6"><span class="toc-text">方法1：给浮动元素的祖先元素加高度</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#%E6%96%B9%E6%B3%952%EF%BC%9Aclear-both"><span class="toc-text">方法2：clear:both;</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#%E6%96%B9%E6%B3%953%EF%BC%9A%E9%9A%94%E5%A2%99%E6%B3%95"><span class="toc-text">方法3：隔墙法</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#%E6%B8%85%E9%99%A4%E6%B5%AE%E5%8A%A8%E6%96%B9%E6%B3%954%EF%BC%9Aoverflow-hidden"><span class="toc-text">清除浮动方法4：overflow:hidden;</span></a></li></ol></li><li class="toc-item toc-level-2"><a class="toc-link" href="#%E6%B5%AE%E5%8A%A8%E6%B8%85%E9%99%A4%E7%9A%84%E6%80%BB%E7%BB%93"><span class="toc-text">浮动清除的总结</span></a><ol class="toc-child"><li class="toc-item toc-level-3"><a class="toc-link" href="#1%E3%80%81%E5%8A%A0%E9%AB%98%E6%B3%95"><span class="toc-text">1、加高法</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#2%E3%80%81clear-both-%E6%B3%95"><span class="toc-text">2、clear:both;法</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#3%E3%80%81%E9%9A%94%E5%A2%99%E6%B3%95"><span class="toc-text">3、隔墙法</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#4%E3%80%81overflow-hidden"><span class="toc-text">4、overflow:hidden;</span></a></li></ol></li><li class="toc-item toc-level-2"><a class="toc-link" href="#%E6%B5%8F%E8%A7%88%E5%99%A8%E7%9A%84%E5%85%BC%E5%AE%B9%E6%80%A7%E9%97%AE%E9%A2%98"><span class="toc-text">浏览器的兼容性问题</span></a><ol class="toc-child"><li class="toc-item toc-level-3"><a class="toc-link" href="#%E5%85%BC%E5%AE%B9%E6%80%A71%EF%BC%88%E5%BE%AE%E5%9E%8B%E7%9B%92%E5%AD%90%EF%BC%89"><span class="toc-text">兼容性1（微型盒子）</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#%E5%85%BC%E5%AE%B9%E6%80%A72"><span class="toc-text">兼容性2</span></a></li></ol></li><li class="toc-item toc-level-2"><a class="toc-link" href="#margin%E7%9B%B8%E5%85%B3"><span class="toc-text">margin相关</span></a><ol class="toc-child"><li class="toc-item toc-level-3"><a class="toc-link" href="#margin%E5%A1%8C%E9%99%B7-x2F-margin%E9%87%8D%E5%8F%A0"><span class="toc-text">margin塌陷&#x2F;margin重叠</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#%E7%9B%92%E5%AD%90%E5%B1%85%E4%B8%ADmargin-0-auto"><span class="toc-text">盒子居中margin:0 auto;</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#%E5%96%84%E4%BA%8E%E4%BD%BF%E7%94%A8%E7%88%B6%E4%BA%B2%E7%9A%84padding%EF%BC%8C%E8%80%8C%E4%B8%8D%E6%98%AF%E5%84%BF%E5%AD%90%E7%9A%84margin"><span class="toc-text">善于使用父亲的padding，而不是儿子的margin</span></a></li></ol></li><li class="toc-item toc-level-2"><a class="toc-link" href="#%E5%85%B3%E4%BA%8Emargin%E7%9A%84IE6%E5%85%BC%E5%AE%B9%E9%97%AE%E9%A2%98"><span class="toc-text">关于margin的IE6兼容问题</span></a><ol class="toc-child"><li class="toc-item toc-level-3"><a class="toc-link" href="#IE6%E7%9A%84%E5%8F%8C%E5%80%8Dmargin%E7%9A%84bug%EF%BC%9A"><span class="toc-text">IE6的双倍margin的bug：</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#IE6%E7%9A%843px-bug"><span class="toc-text">IE6的3px bug</span></a></li></ol></li><li class="toc-item toc-level-2"><a class="toc-link" href="#Fireworks%E5%92%8Cothers"><span class="toc-text">Fireworks和others</span></a><ol class="toc-child"><li class="toc-item toc-level-3"><a class="toc-link" href="#Fireworks"><span class="toc-text">Fireworks</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#others"><span class="toc-text">others</span></a></li></ol></li><li class="toc-item toc-level-2"><a class="toc-link" href="#%E6%88%91%E7%9A%84%E5%85%AC%E4%BC%97%E5%8F%B7"><span class="toc-text">我的公众号</span></a></li></ol>
    </article>
  
  
  <article class="card card-content">
    <div class="recent-posts-card">
  <div class="recent-posts-header">
    <i 
      class="iconfont icon-wenzhang_huaban" 
      style="padding-right: 2px;">
    </i>最近文章
  </div>
  <div class="recent-posts-list">
    
      <div class="recent-posts-item">
        <div class="recent-posts-item-title">2023-03-09</div>
        <a href="/2023/03/09/13-React%E5%9F%BA%E7%A1%80/11-React%20Navive%E5%88%9D%E8%AF%86/"><div class="recent-posts-item-content">11-React Navive初识</div></a>
      </div>
    
      <div class="recent-posts-item">
        <div class="recent-posts-item-title">2023-03-09</div>
        <a href="/2023/03/09/13-React%E5%9F%BA%E7%A1%80/10-AntD%E6%A1%86%E6%9E%B6%E7%9A%84upload%E7%BB%84%E4%BB%B6%E4%B8%8A%E4%BC%A0%E5%9B%BE%E7%89%87%E6%97%B6%E4%BD%BF%E7%94%A8customRequest%E6%96%B9%E6%B3%95%E8%87%AA%E5%AE%9A%E4%B9%89%E4%B8%8A%E4%BC%A0%E8%A1%8C%E4%B8%BA/"><div class="recent-posts-item-content">10-AntD框架的upload组件上传图片时使用customRequest方法自定义上传行为</div></a>
      </div>
    
      <div class="recent-posts-item">
        <div class="recent-posts-item-title">2023-03-09</div>
        <a href="/2023/03/09/13-React%E5%9F%BA%E7%A1%80/09-AntD%E6%A1%86%E6%9E%B6%E7%9A%84upload%E7%BB%84%E4%BB%B6%E4%B8%8A%E4%BC%A0%E5%9B%BE%E7%89%87%E6%97%B6%E9%81%87%E5%88%B0%E7%9A%84%E4%B8%80%E4%BA%9B%E5%9D%91/"><div class="recent-posts-item-content">09-AntD框架的upload组件上传图片时遇到的一些坑</div></a>
      </div>
    
      <div class="recent-posts-item">
        <div class="recent-posts-item-title">2023-03-09</div>
        <a href="/2023/03/09/13-React%E5%9F%BA%E7%A1%80/08-Ant%20Design%E7%9A%84%E5%9F%BA%E6%9C%AC%E4%BD%BF%E7%94%A8/"><div class="recent-posts-item-content">08-Ant Design的基本使用</div></a>
      </div>
    
  </div>
</div>
  </article>
  
  
</div>
            </aside>
          </div>
        </div>
      </div>
    </div>
     
    <footer class="footer">
  <div class="footer-container">
    <div>
      <div class="footer-dsc">
        <span>
          Copyright ©
          
            2023
          
          
        </span>
        &nbsp;
        <a 
          href="/" 
          class="footer-link">
          前端面试
        </a>
      </div>
    </div>

    
      <div class="footer-dsc">
        
          Powered by
          <a 
            href="https://hexo.io/" 
            class="footer-link" 
            target="_blank" 
            rel="nofollow noopener noreferrer">
            &nbsp;Hexo
          </a>
        
        
          <span>&nbsp;|&nbsp;</span>
        
        
          Theme -
          <a 
            href="https://github.com/theme-kaze" 
            class="footer-link" 
            target="_blank"
            rel="nofollow noopener noreferrer">
            &nbsp;Kaze
          </a>
        
      </div>
    
    
    
    
</footer>
 
    
  <a 
    role="button" 
    id="scrollbutton" 
    class="basebutton" 
    aria-label="回到顶部">
    <i class="iconfont icon-arrowleft button-icon"></i>
  </a>

<a 
  role="button" 
  id="menubutton"
  aria-label="menu button"
  class="basebutton">
  <i class="iconfont icon-menu button-icon"></i>
</a>
<a 
  role="button" 
  id="popbutton" 
  class="basebutton" 
  aria-label="控制中心">
  <i class="iconfont icon-expand button-icon"></i>
</a>
<a 
  role="button" 
  id="darkbutton" 
  class="basebutton darkwidget" 
  aria-label="夜色模式">
  <i class="iconfont icon-weather button-icon"></i>
</a>
<a 
  role="button" 
  id="searchbutton" 
  class="basebutton searchwidget" 
  aria-label="搜索">
  <i class="iconfont icon-search button-icon"></i>
</a> 
     
     
     
      <script>
  var addImgLayout = function () {
    var img = document.querySelectorAll('.post-content img')
    var i
    for (i = 0; i < img.length; i++) {
      var wrapper = document.createElement('a')
      wrapper.setAttribute('href', img[i].getAttribute('data-src'))
      wrapper.setAttribute('aria-label', 'illustration')
      wrapper.style.cssText =
        'width: 100%; display: flex; justify-content: center;'
      if (img[i].alt) wrapper.dataset.caption = img[i].alt
      wrapper.dataset.nolink = true
      img[i].before(wrapper)
      wrapper.append(img[i])
      var divWrap = document.createElement('div')
      divWrap.classList.add('gallery')
      wrapper.before(divWrap)
      divWrap.append(wrapper)
    }
    baguetteBox.run('.gallery')
  }
</script>
<script>
  loadScript(
    "/js/lib/lightbox/baguetteBox.min.js",
    addImgLayout
  )
</script>
 
     
     
    <script src="/js/main.js"></script> 
     
    
      <script>
        var addLazyload = function () {
          var observer = lozad('.lozad', {
            load: function (el) {
              el.srcset = el.getAttribute('data-src')
            },
            loaded: function (el) {
              el.classList.add('loaded')
            },
          })
          observer.observe()
        }
      </script>
      <script>
        loadScript('/js/lib/lozad.min.js', addLazyload)
      </script>
    
    <script src="//instant.page/5.1.0" type="module"
      integrity="sha384-by67kQnR+pyfy8yWP4kPO12fHKRLHZPfEsiSXR8u2IKcTdxD805MGUXBzVPnkLHw"></script>
    
    
  </body>
</html>
